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

From: Date: Wed, 18 Mar 2015 19:38:58 +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-191453@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:

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


Previous Comments:
------------------------------------------------------------------------
[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...

------------------------------------------------------------------------
[2012-09-11 19:13:10] chris dot baker dot gr at gmail dot com

To amend my earlier comment: my repro attempts show that if I create an instance of DateTime by
passing a string to the constructor, everything works fine. If I use a timestamp in combination with
DateTime::createFromFormat, I see the bug affect the results

Use of a string
-------------------------	
$start = new DateTime('09/13/2012 7:00pm');
$end = new DateTime('09/13/2012 8:00pm');
$interval = DateInterval::createFromDateString('30 minutes');

// expected, 1 hours, 0 minutes, get 1 hours, 0 minutes
$diff = $start->diff($end);
print 'Diff: '.$diff->h.' hours, '.$diff->i.'
minutes'."\n";

$end->add($interval);
$diff = $start->diff($end);
// ecpected: 1 hours, 30 minutes, get 1 hours, 30 minutes
print "\n".'Diff after interval: '.$diff->h.' hours,
'.$diff->i.' minutes';


Use of a timestamp
-------------------------

$start = DateTime::createFromFormat('U', 1347577200);
$end =  DateTime::createFromFormat('U', 1347580800);
$interval = DateInterval::createFromDateString('30 minutes');

// expected, 1 hours, 0 minutes, get 1 hours, 0 minutes
$diff = $start->diff($end);
print 'Diff: '.$diff->h.' hours, '.$diff->i.'
minutes'."\n";

$end->add($interval);
$diff = $start->diff($end);
// ecpected: 1 hours, 30 minutes, get 2 hours, 30 minutes
print "\n".'Diff after interval: '.$diff->h.' hours,
'.$diff->i.' minutes';

http://3v4l.org/fivbW

The extra one hour makes less sense considering that my server timezone is -5 UTC -- not that it
makes sense for DateInterval to be sensitive to timezone!

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


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 (#191453) next »