如何检查存储在数据库中的时间是否超过两个小时


How to check if the time stored in database exceeded two hours

每次单击密码重置请求链接时,我都会使用 PHPfunction now()以这种形式将时间存储到用户表中:

2015-06-15 12:02:09

如果日期和时间超过 2 小时,如何检查日期和时间?一个简单的2*60*60会有所帮助吗?

还要考虑的日期..如果他们在指定时间之后和之前一天点击了链接怎么办?

如果你想用PHP来做,strtotime()就是你要找的。查看您尝试过的代码会很有帮助。

<?php
$timediff = strtotime('now') - strtotime('2015-06-15 00:02:09'); // in seconds
if( $timediff > 7200 ) { // 7200 seconds is 2 hours
    // do stuff;
}
?>

这将按照您的要求考虑日期,因为 strtotime 函数返回自纪元时间(1970 年 1 月 1 日)以来经过的时间量(以秒为单位)。

mysql 有自己的一组日期函数

SELECT pasword_link_stuff from table where (NOW() - INTERVAL 2 HOUR) < date_field

你可以做这样的事情,

select * from table where (time_to_sec(timediff('2015-06-15 12:02:09', CURRENT_TIMESTAMP )) / 3600) > 2