dongxiao0449 2011-09-08 14:43
浏览 96
已采纳

php sql日期时间比较

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.

  • 写回答

3条回答 默认 最新

  • dpcj40970 2011-09-08 14:51
    关注

    You can use sql like this:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?