SQL / PHP将两个日期比较为秒

时间:2015-07-01 04:16:19

标签: php mysql date time

我有一个名为 lastTime 的字段,它保存当我使用NOW()查询我的表时的当前时间。 它已在我的表格中存储为日期时间

然后我使用查询从表中的某个点检索此值,并返回一个字符串。

$lastTime = retrieve_lasttime_from_table();
$curTime = date("Y-m-d H:i:s");

if($curTime - $lastTime >= 5){
    //Do stuff here
}

所以基本上我试图看看是否已经过了5秒钟。 这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

您可以使用strtotime,以下示例可能会有效。

if(strtotime($lastTime) - (strtotime($curTime)) >= 5){

if((strtotime($curTime) - strtotime($lastTime)) >= 5){
    //Do stuff here
}

注意:我切换了$lastTime$curTime的位置,因为我猜您需要检查当前时间是否大于或等于上次的5秒

答案 1 :(得分:2)

strtotime()是找到差异的最佳方式

if((strtotime($curTime) - strtotime($lastTime)) >= 5){
    //Do stuff here
}