I have the following code that receives the user a start date ( $dtStart
) and another end date ( $dtEnd
). The goal is to do an hourly query in mysql between these two dates.
I need to control the process in a loop in php (because other events run between those minutes).
//Nesse exemplo deveria haver 24hs
$dtStart="2018-01-01";
$dtEnd ="2018-01-01";
$dtTimeStart=$dtStart." 00:00:00";
$dtTimeEnd =$dtEnd ." 23:59:59";
$hourdiff = round((strtotime($dtTimeEnd) - strtotime($dtTimeStart))/3600, 1);
echo "diffHours=".$hourdiff;//24
$x=1;
while($x <= $hourdiff) {
echo "Hour: $x <br>";
if($x==1){
$dtStrt = $dtTimeStart;
}else{
$dtStrt = $dtEnd;
}
$date = new DateTime($dtStrt);
$dtTemp = $date->modify('+1 hour');
$dtEnd = $dtTemp->format('Y-m-d H:m:s');
echo "=======================================================<br>";
echo "dtStrt = ".$dtStrt."<br>";
echo "dtEnd = ".$dtEnd."<br>";
echo "=======================================================<br>";
echo "</br>";
$x++;
}
exit;
And this is what I have in the browser as a result of the operation:
Hour: 1
=======================================================
dtStrt = 2018-01-01 00:00:00
dtEnd = 2018-01-01 01:01:00
=======================================================
Hour: 2
=======================================================
dtStrt = 2018-01-01 01:01:00
dtEnd = 2018-01-01 02:01:00
=======================================================
Hour: 3
=======================================================
dtStrt = 2018-01-01 02:01:00
dtEnd = 2018-01-01 03:01:00
=======================================================
Hour: 4
=======================================================
dtStrt = 2018-01-01 03:01:00
dtEnd = 2018-01-01 04:01:00
=======================================================
Hour: 5
=======================================================
dtStrt = 2018-01-01 04:01:00
dtEnd = 2018-01-01 05:01:00
=======================================================
Hour: 6
=======================================================
dtStrt = 2018-01-01 05:01:00
dtEnd = 2018-01-01 06:01:00
=======================================================
Hour: 7
=======================================================
dtStrt = 2018-01-01 06:01:00
dtEnd = 2018-01-01 07:01:00
=======================================================
Hour: 8
=======================================================
dtStrt = 2018-01-01 07:01:00
dtEnd = 2018-01-01 08:01:00
=======================================================
Hour: 9
=======================================================
dtStrt = 2018-01-01 08:01:00
dtEnd = 2018-01-01 09:01:00
=======================================================
Hour: 10
=======================================================
dtStrt = 2018-01-01 09:01:00
dtEnd = 2018-01-01 10:01:00
=======================================================
Hour: 11
=======================================================
dtStrt = 2018-01-01 10:01:00
dtEnd = 2018-01-01 11:01:00
=======================================================
Hour: 12
=======================================================
dtStrt = 2018-01-01 11:01:00
dtEnd = 2018-01-01 12:01:00
=======================================================
Hour: 13
=======================================================
dtStrt = 2018-01-01 12:01:00
dtEnd = 2018-01-01 13:01:00
=======================================================
Hour: 14
=======================================================
dtStrt = 2018-01-01 13:01:00
dtEnd = 2018-01-01 14:01:00
=======================================================
Hour: 15
=======================================================
dtStrt = 2018-01-01 14:01:00
dtEnd = 2018-01-01 15:01:00
=======================================================
Hour: 16
=======================================================
dtStrt = 2018-01-01 15:01:00
dtEnd = 2018-01-01 16:01:00
=======================================================
Hour: 17
=======================================================
dtStrt = 2018-01-01 16:01:00
dtEnd = 2018-01-01 17:01:00
=======================================================
Hour: 18
=======================================================
dtStrt = 2018-01-01 17:01:00
dtEnd = 2018-01-01 18:01:00
=======================================================
Hour: 19
=======================================================
dtStrt = 2018-01-01 18:01:00
dtEnd = 2018-01-01 19:01:00
=======================================================
Hour: 20
=======================================================
dtStrt = 2018-01-01 19:01:00
dtEnd = 2018-01-01 20:01:00
=======================================================
Hour: 21
=======================================================
dtStrt = 2018-01-01 20:01:00
dtEnd = 2018-01-01 21:01:00
=======================================================
Hour: 22
=======================================================
dtStrt = 2018-01-01 21:01:00
dtEnd = 2018-01-01 22:01:00
=======================================================
Hour: 23
=======================================================
dtStrt = 2018-01-01 22:01:00
dtEnd = 2018-01-01 23:01:00
=======================================================
Hour: 24
=======================================================
dtStrt = 2018-01-01 23:01:00
dtEnd = 2018-01-02 00:01:00
=======================================================
As you can see the date is incremented by +1 hour and + 1min. And the last hour is going through January 2 (in the example) and +1 min. That is, we have in total 24 + 1min. The expected would be only 24 hours.
Any ideas how to fix this?