Php时间比较

时间:2014-09-09 09:06:16

标签: php mysql strtotime

我使用以下代码向用户发送一些警报。例如,如果用户的旅行日期是04:00:00,则他必须从03:00:00到06:00:00接收警报。这是零下一小时,增加了2个小时。如何让这些记录落入这个窗口。

我尝试了不同时间窗的组合,但我无法做到。

$time = date('Y-m-d H:i:s');
$time_1 = strtotime($time) - 3600; // minus 1 hour to current time
$time_2 = strtotime($time) + 7200; // Add 2 hours to current time
$time_to_compare1 = date('Y-m-d H:i:s', $time_1); 
$time_to_compare2 = date('Y-m-d H:i:s', $time_2);
select * FROM  `journey_tracker` where push='0' and batch = '$batch' and doj between      '$time_to_compare1' and '$time_to_compare2' 

以上代码失败,因为它返回时间为05:00:00或06:00:00的记录

1 个答案:

答案 0 :(得分:2)

SELECT *
FROM `journey_tracker`
WHERE push='0'
  AND batch = '$batch'
  AND doj BETWEEN DATE_ADD(doj, INTERVAL -1 HOUR) AND DATE_ADD(doj, INTERVAL 2 HOUR)