Jump to content

Problem with strtotime


Warrigal
Go to solution Solved by Warrigal,

Recommended Posts

I have a script which calculates how many hours a truck driver has worked/rested in increments of 15 minutes.

 

For a given number of hours I loop around adding 15 minutes to a cursor for each 15 minutes of work/rest.

 

This all works fine until 7 Oct 2012 when adding 15 minutes to 01:45:00 gives a result of 03:00:00 instead of 02:00:00

 

Here's my code:

<?php

$diff = 4; // Number of hours difference in units of 15 minutes
$cursor = "2012-10-07 00:00:00"; // Start time, midnight on 07 October 2012
for($z=0;$z<$diff/.25;$z++) {
	print "Cursor = ".$cursor."<br>";
	$cursor = date('Y-m-d H:i:s', strtotime( "$cursor + 15 minutes" ));
}
?>

This is the result:

 

Cursor = 2012-10-07 00:00:00
Cursor = 2012-10-07 00:15:00
Cursor = 2012-10-07 00:30:00
Cursor = 2012-10-07 00:45:00
Cursor = 2012-10-07 01:00:00
Cursor = 2012-10-07 01:15:00
Cursor = 2012-10-07 01:30:00
Cursor = 2012-10-07 01:45:00 /* Jumps to 03:00:00 instead of 02:00:00 */
Cursor = 2012-10-07 03:00:00
Cursor = 2012-10-07 03:15:00
Cursor = 2012-10-07 03:30:00
Cursor = 2012-10-07 03:45:00
Cursor = 2012-10-07 04:00:00
Cursor = 2012-10-07 04:15:00
Cursor = 2012-10-07 04:30:00
Cursor = 2012-10-07 04:45:00

 

Can anyone help??

Link to comment
Share on other sites

try

$dt = new DateTime("2012-10-07 00:00:00");
$di = new DateInterval('PT15M');
$dp = new DatePeriod($dt, $di, 16);

foreach ($dp as $d) {
    echo $d->format('Y-m-d H:i') . '<br>';
}

Output

2012-10-07 00:00
2012-10-07 00:15
2012-10-07 00:30
2012-10-07 00:45
2012-10-07 01:00
2012-10-07 01:15
2012-10-07 01:30
2012-10-07 01:45
2012-10-07 02:00
2012-10-07 02:15
2012-10-07 02:30
2012-10-07 02:45
2012-10-07 03:00
2012-10-07 03:15
2012-10-07 03:30
2012-10-07 03:45
2012-10-07 04:00
Link to comment
Share on other sites

Thanks Barand!

 

Unfortunately I am using php 5.2.17 and DateInterval was introduced in 5.3 - I will have to update my server and try again.

 

Still curious about the glitch with strtotime though - seems to work with every other date and time except 2012-10-07.

 

Link to comment
Share on other sites

I ran your code for 2013-10-27 (day UK DST changes) and still got

Cursor = 2013-10-27 00:00:00
Cursor = 2013-10-27 00:15:00
Cursor = 2013-10-27 00:30:00
Cursor = 2013-10-27 00:45:00
Cursor = 2013-10-27 01:00:00
Cursor = 2013-10-27 01:15:00
Cursor = 2013-10-27 01:30:00
Cursor = 2013-10-27 01:45:00
Cursor = 2013-10-27 02:00:00
Cursor = 2013-10-27 02:15:00
Cursor = 2013-10-27 02:30:00
Cursor = 2013-10-27 02:45:00
Cursor = 2013-10-27 03:00:00
Cursor = 2013-10-27 03:15:00
Cursor = 2013-10-27 03:30:00
Cursor = 2013-10-27 03:45:00

so that kills the DST theory

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.