开发者

php sql date time comparison

I have a last_notified column on my MySQL table, data type DATETIME I want to either only select the rows where last_notified is more than n seconds old, or compare the last_notified value from the db in php.

I have tried messing around with the date object.

$d = date('Y-m-d H:i:s'); // current date and time
$dd = date('2011-09-08 10:21:34'); // this is the format used in mysql.

I know I cannot just compare them, and i'm unaware 开发者_如何学编程of how to add time to the date object. I have seen examples of people using something along the lines of

$t = explode(" ",$dd);
date($dd, strtotime('+30 minutes', strtotime($t[1])));

but that doesn't work . I'm just not seeing it.


You can use sql like this:

SELECT * FROM your_table WHERE last_notified < DATE_SUB(NOW(), INTERVAL n SECOND)


Take a look at your first snippet and date mask in first line

Y-m-d H:m:s

you use m mask twice, so it means there will be month instead of minutes. you should use i mask to specify the minutes


SELECT * FROM your_table WHERE last_notified < DATE_SUB(NOW(), INTERVAL n SECOND)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜