Warrigal Posted September 8, 2013 Share Posted September 8, 2013 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:00Cursor = 2012-10-07 00:15:00Cursor = 2012-10-07 00:30:00Cursor = 2012-10-07 00:45:00Cursor = 2012-10-07 01:00:00Cursor = 2012-10-07 01:15:00Cursor = 2012-10-07 01:30:00Cursor = 2012-10-07 01:45:00 /* Jumps to 03:00:00 instead of 02:00:00 */Cursor = 2012-10-07 03:00:00Cursor = 2012-10-07 03:15:00Cursor = 2012-10-07 03:30:00Cursor = 2012-10-07 03:45:00Cursor = 2012-10-07 04:00:00Cursor = 2012-10-07 04:15:00Cursor = 2012-10-07 04:30:00Cursor = 2012-10-07 04:45:00 Can anyone help?? Link to comment https://forums.phpfreaks.com/topic/281968-problem-with-strtotime/ Share on other sites More sharing options...
Barand Posted September 8, 2013 Share Posted September 8, 2013 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 https://forums.phpfreaks.com/topic/281968-problem-with-strtotime/#findComment-1448659 Share on other sites More sharing options...
Warrigal Posted September 8, 2013 Author Share Posted September 8, 2013 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 https://forums.phpfreaks.com/topic/281968-problem-with-strtotime/#findComment-1448660 Share on other sites More sharing options...
Barand Posted September 8, 2013 Share Posted September 8, 2013 Change in daylight saving time Link to comment https://forums.phpfreaks.com/topic/281968-problem-with-strtotime/#findComment-1448661 Share on other sites More sharing options...
Barand Posted September 8, 2013 Share Posted September 8, 2013 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 https://forums.phpfreaks.com/topic/281968-problem-with-strtotime/#findComment-1448663 Share on other sites More sharing options...
Warrigal Posted September 8, 2013 Author Share Posted September 8, 2013 Problem appears to be fixed in php >=5.3 Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/281968-problem-with-strtotime/#findComment-1448671 Share on other sites More sharing options...
Warrigal Posted September 9, 2013 Author Share Posted September 9, 2013 Barand, you were right about DST - this only worked in 5.3 until I set the timezone to Australia/Sydney - DST started on 7th October 2012. It gets handled correctly for all other years that I have tested - I'll dig around some more and let you know. Link to comment https://forums.phpfreaks.com/topic/281968-problem-with-strtotime/#findComment-1448900 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.