How to increment +1h between two dates using php?

Asked

Viewed 147 times

0

I have the following code that receives from the user an initial date ($dtStart) and another final date ($dtEnd). The goal is to query every hour in mysql between these two dates.

I need to control the process in a loop in php (as other events run between these 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 that’s 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 observed, the date is incremented by +1 hour and + 1 min. And the last hour is going up to the second of January (in the example) and +1 min. That is, we have a total of 24+1 min. The expected would only be 24hs.

Any idea how to fix this?

1 answer

3


The code is correct, the problem is that you are capturing the month m, instead of minutes i.

The mistake is in:

$dtEnd  = $dtTemp->format('Y-m-d H:m:s');

The correct is:

$dtEnd  = $dtTemp->format('Y-m-d H:i:s');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.