Reputation: 2935
I am using the following code to send some alerts to my users. For instance if a user's date of journey is at 04:00:00, he has to receive alerts from 03:00:00 to 06:00:00. That is minus one hour and adding 2 hours. How can i get the records falling in this window.
I tried different combination of time windows but i couldnt make it.
$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'
The above code fails because it returns a record with time of journey 05:00:00 or 06:00:00
Upvotes: 0
Views: 118
Reputation: 18598
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)
Upvotes: 2