开发者

PHP MySQL Date diff

开发者 https://www.devze.com 2023-01-19 01:51 出处:网络
开发者_StackOverflowI have the field datetime with DATETIME type in MySQL. In PHP script I set date begin and date end like this: 11/12/1999 and 11/12/2001. In my table datetime saved in the next form
开发者_StackOverflow

I have the field datetime with DATETIME type in MySQL. In PHP script I set date begin and date end like this: 11/12/1999 and 11/12/2001. In my table datetime saved in the next format: 11.11.1888 00:00:00. How can I compare these dates?

Thanks.


It could be done more easly, but this is one way:

$begindate = strreplace($begindate,'/','.');
$enddate = strreplace($enddate,'/','.');

mysql_query("SELECT * FROM MyTable
WHERE dateField BETWEEN '$begindate 00:00:00' AND '$enddate 23:59:59'");


should be something like

SELECT *, STR_TO_DATE(datetime,'%d.%m.%Y') as newdatetime
FROM table
WHERE newdatetime >= STR_TO_DATE('11/12/1999','%d/%m/%Y')
AND newdatetime <= STR_TO_DATE('11/12/2001','%d/%m/%Y');
0

精彩评论

暂无评论...
验证码 换一张
取 消