Bug #61955 [Com]: Adding DateInterval to DateTime adds 1 additional hour

From: Date: Wed, 18 Mar 2015 20:11:39 +0000
Subject: Bug #61955 [Com]: Adding DateInterval to DateTime adds 1 additional hour
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191454@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=61955&edit=1

 ID:                 61955
 Comment by:         floridia at gmail dot com
 Reported by:        php at arjanonline dot net
 Summary:            Adding DateInterval to DateTime adds 1 additional
                     hour
 Status:             Assigned
 Type:               Bug
 Package:            Date/time related
 Operating System:   Linux / Mac
 PHP Version:        5.3.12
 Assigned To:        derick
 Block user comment: N
 Private report:     N

 New Comment:

Got it. It is not really a bug in my case. That day is the turning point of DST. To work around that
behavior I disabled the DST, changing the timezone to a neutral one (Etc/GMT-3) instead of a
political one (America/Sao Paulo). More detail in Qualcuno's answer here:
http://stackoverflow.com/questions/25478998/disable-daylight-saving-functionality


Previous Comments:
------------------------------------------------------------------------
[2015-03-18 19:38:57] floridia at gmail dot com

Related with DST Change:
https://bugs.php.net/bug.php?id=51051

------------------------------------------------------------------------
[2015-03-18 19:31:01] floridia at gmail dot com

Similiar issue:

The following code doesn't work with particular date 2015-02-21 23:45:00 when trying to add 15
minutes...


$date1 = new DateTime('2015-02-21 23:45:00');
$interval = new DateInterval('PT15M');
print_r($date1);
$date1->add($interval);
print_r($date1);

Output
DateTime Object
(
    [date] => 2015-02-21 23:45:00.000000
    [timezone_type] => 3
    [timezone] => America/Sao_Paulo
)
DateTime Object
(
    [date] => 2015-02-21 23:00:00.000000
    [timezone_type] => 3
    [timezone] => America/Sao_Paulo
)

Expected Output
DateTime Object
(
    [date] => 2015-02-22 00:00:00.000000
    [timezone_type] => 3
    [timezone] => America/Sao_Paulo
)

------------------------------------------------------------------------
[2013-05-05 12:10:19] jack at jtom dot me

This bug is still up. I'm experiencing it on PHP 5.4.3 and Windows 8 x64 platform. 
I had to have 'Europe/Warsaw' set as the default timezone to make the bug occur.

Currently I managed to handle this bug by switching the default timezone to UTC 
every time I make date->add/sub/modify , but it's definitely annoying :(

------------------------------------------------------------------------
[2012-11-24 01:15:25] whistl0r+php at googlemail dot com

I think this bug was introduced with http://svn.php.net/viewvc/?view=revision&revision=319767

------------------------------------------------------------------------
[2012-11-24 00:13:09] whistl0r+php at googlemail dot com

This bug seems to be still present in PHP 5.3.19. My test script:

<?php
// The local system is set to Europe/Berlin timezone, so set it for PHP, too:
date_default_timezone_set( 'Europe/Berlin');
echo 'Current date: ' . date('c') . PHP_EOL;

// Now, we are switching to UTC...
date_default_timezone_set( 'UTC');

$timezone = new DateTimeZone( 'UTC' );
$datetime = new DateTime( null, $timezone );
$datetime->setTime( 00, 00, 00 );
$datetime->add( new DateInterval( 'P1D' ) );

// Output
echo 'mktime():    ' . ( mktime( 23, 59, 59 ) + 1 ) . PHP_EOL;
echo 'gmmktime():  ' . ( gmmktime( 23, 59, 59 ) + 1 ) . PHP_EOL;
echo 'datetime():  ' . $datetime->getTimestamp() . PHP_EOL;
echo 'time():      ' . time() . PHP_EOL;
echo 'microtime(): ' . microtime( true ) . PHP_EOL;
echo 'Used TZ:     ' . date_default_timezone_get() . PHP_EOL;
?>

I run this test script at 2012-11-24T00:00:00+01:00. This is my output:

Current date: 2012-11-24T00:00:00+01:00
mktime():    1353715200
gmmktime():  1353715200
datetime():  1353715200
time():      1353711600
microtime(): 1353711600.7557
Used TZ:     UTC

I am expecting that at least the DateTime value should be equal to time(), because both functions
should represent a UNIX timestamp (which is per definition UTC).

But I am also not sure if the gmmktime() *and* mktime() should be equal... and I don't
understand the 3600 offset at all - remember, we set UTC timezone before...

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=61955


--
Edit this bug report at https://bugs.php.net/bug.php?id=61955&edit=1


Thread (14 messages)

« previous php.bugs (#191454) next »