简体   繁体   English

PHP的SQL日期时间比较

[英]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. 我的MySQL表上有一个last_notified列,数据类型为DATETIME我只想选择last_notified超过n秒的行,或者比较php中db的last_notified值。

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. 我只是没有看到它。

您可以像这样使用sql:

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. 您使用m mask两次,所以这意味着将有一个月而不是分钟。 you should use i mask to specify the minutes 你应该用i面具来指定分钟

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM