Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/213774-datetime-loses-an-hour/
Share on other sites

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

 

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?

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

 

 

 

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

 

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.