hidden_premise Posted September 19, 2010 Share Posted September 19, 2010 Hi all, I have a loop that is modifying a DateTime object. It goes through X number of times and adds 1 day to the date time object and stores the modified object in an array. I use ->add(new DateInterval('P1D')); For some reason it is losing an hour each time that I add a day. I start out at 19:00 and each iteration through the loop adds a day, but loses an hour. 2010-09-19 19:00:00 2010-09-20 18:00:00 2010-09-21 17:00:00 I can correct for this by doing ->add(new DateInterval('P1DT1H'));, but adding 25 hours instead of 24 hours doesn't make any sense to me. I am using PHP Version 5.3.1. Any ideas? Thanks, W Quote Link to comment https://forums.phpfreaks.com/topic/213774-datetime-loses-an-hour/ Share on other sites More sharing options...
jcbones Posted September 19, 2010 Share Posted September 19, 2010 My test server is PHP 5.3.0 <?php $time = new DateTime(); for($i = 0; $i < 10; $i++) { echo $time->format('m-d-Y H:i:s') . '<br />'; $time->add(new DateInterval('P1D')); } ?> Outputs: 09-18-2010 22:11:44 09-19-2010 22:11:44 09-20-2010 22:11:44 09-21-2010 22:11:44 09-22-2010 22:11:44 09-23-2010 22:11:44 09-24-2010 22:11:44 09-25-2010 22:11:44 09-26-2010 22:11:44 09-27-2010 22:11:44 Quote Link to comment https://forums.phpfreaks.com/topic/213774-datetime-loses-an-hour/#findComment-1112673 Share on other sites More sharing options...
hidden_premise Posted September 20, 2010 Author Share Posted September 20, 2010 Here is what I'm seeing echo "<BR/><BR/>LOOPING - BEFORE begin: " . $date_begin->format(DATE_FORMAT) . " end: " . $date_end->format(DATE_FORMAT); $date_begin->add(new DateInterval('P1D')); $date_end->add(new DateInterval('P1D')); echo "<BR/>LOOPING - AFTER begin: " . $date_begin->format(DATE_FORMAT) . " end: " . $date_end->format(DATE_FORMAT); output LOOPING - BEFORE begin: 09/16/10 07:00 PM end: 09/16/10 11:50 PM LOOPING - AFTER begin: 09/17/10 06:00 PM end: 09/17/10 10:50 PM Could this be some bizarre timezone issue? Quote Link to comment https://forums.phpfreaks.com/topic/213774-datetime-loses-an-hour/#findComment-1113458 Share on other sites More sharing options...
btherl Posted September 21, 2010 Share Posted September 21, 2010 What happens if you use 24 hours instead of 1 day? Quote Link to comment https://forums.phpfreaks.com/topic/213774-datetime-loses-an-hour/#findComment-1113578 Share on other sites More sharing options...
hidden_premise Posted September 21, 2010 Author Share Posted September 21, 2010 echo "<BR/><BR/>LOOPING - BEFORE begin: " . $date_begin->format(DATE_FORMAT) . " end: " . $date_end->format(DATE_FORMAT); $date_begin->add(new DateInterval('PT24H')); $date_end->add(new DateInterval('PT24H')); echo "<BR/>LOOPING - AFTER begin: " . $date_begin->format(DATE_FORMAT) . " end: " . $date_end->format(DATE_FORMAT); LOOPING - BEFORE begin: 09/16/10 07:00 PM end: 09/16/10 11:50 PM LOOPING - AFTER begin: 09/17/10 06:00 PM end: 09/17/10 10:50 PM if I make it 25 hours though or 1 day 1 hour echo "<BR/><BR/>LOOPING - BEFORE begin: " . $date_begin->format(DATE_FORMAT) . " end: " . $date_end->format(DATE_FORMAT); $date_begin->add(new DateInterval('PT25H')); $date_end->add(new DateInterval('PT25H')); echo "<BR/>LOOPING - AFTER begin: " . $date_begin->format(DATE_FORMAT) . " end: " . $date_end->format(DATE_FORMAT); LOOPING - BEFORE begin: 09/16/10 07:00 PM end: 09/16/10 11:50 PM LOOPING - AFTER begin: 09/17/10 07:00 PM end: 09/17/10 11:50 PM Quote Link to comment https://forums.phpfreaks.com/topic/213774-datetime-loses-an-hour/#findComment-1113695 Share on other sites More sharing options...
jcbones Posted September 24, 2010 Share Posted September 24, 2010 Here is my test, again PHP 5.3.0 PS. This would help if you would post ALL relevant code. <?php$date_begin = new DateTime('2010-10-12 14:00:23');$date_end = new DateTime('2010-10-13 18:00:23');define('DATE_FORMAT', 'm/d/Y h:i:s A');echo "<BR/><BR/>LOOPING - BEFORE begin: " . $date_begin->format(DATE_FORMAT) . " end: " . $date_end->format(DATE_FORMAT); $date_begin->add(new DateInterval('P1D')); $date_end->add(new DateInterval('P1D')); echo "<BR/>LOOPING - AFTER begin: " . $date_begin->format(DATE_FORMAT) . " end: " . $date_end->format(DATE_FORMAT);?> Output. LOOPING - BEFORE begin: 10/12/2010 02:00:23 PM end: 10/13/2010 06:00:23 PM LOOPING - AFTER begin: 10/13/2010 02:00:23 PM end: 10/14/2010 06:00:23 PM Quote Link to comment https://forums.phpfreaks.com/topic/213774-datetime-loses-an-hour/#findComment-1114831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.