PHP If语句带有两个时间戳

时间:2014-01-21 03:10:00

标签: php mysql

我正在尝试运行PHP if语句,该语句表明两个MySQL时间戳之间的时间间隔是否超过3600秒。我认为我已正确编写了IF语句,但有些事情正在发生,它始终被解析为真。

 $time1=mysqli_query($con,"SELECT time_to_sec(max(time)) as time FROM table");
 $time2=mysqli_query($con,"SELECT time_to_sec(max(time)) as time FROM table and time <>(SELECT max(time) FROM table)");


 if (($time1->fetch_object()->time) - ($time2->fetch_object()->time) > 3600);
 {
 echo $test
 }

当我跑步时:

 echo $time1->fetch_object()->time-$time2->fetch_object()->time;

它显示了我期望的正确值,但即使它小于3600,它仍然会进行echo $ test。可能导致这种情况的任何想法?

谢谢!!!

1 个答案:

答案 0 :(得分:1)

使用 CASE 语句,这是一种更好,更有效的方法:

Select event, case when  TIMESTAMPDIFF(SECOND, last_timstamp, first_timestamp)<3600 then   `under_time` when TIMESTAMPDIFF(last_timstamp, first_timestamp)>3600 then `over_time` end case   as `duration_flag` from your_table where your_conditions

然后在 PHP 中根据查询的标志调整输出。

这样,您可以减少对服务器的请求,并且查询更清晰。

也许 mysql 有一个旧版的TIMESTAMPDIFF错误,所以如果它不起作用,请改用:

Time_To_Sec(last_timstamp) - Time_To_Sec(first_timestamp)