php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63311 DateTime::add() adds wrong interval when switching from summer to winter time
Submitted: 2012-10-19 12:30 UTC Modified: 2017-03-19 10:47 UTC
Votes:7
Avg. Score:4.3 ± 0.7
Reproduced:7 of 7 (100.0%)
Same Version:2 (28.6%)
Same OS:2 (28.6%)
From: thomas dot mayer at 2bis10 dot de Assigned:
Status: Duplicate Package: Date/time related
PHP Version: 7.1 OS: Windows 7, debian 6
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
36 - 33 = ?
Subscribe to this entry?

 
 [2012-10-19 12:30 UTC] thomas dot mayer at 2bis10 dot de
Description:
------------
When adding an interval to a DateTime object in european summer time and the resulting date is in european winter time, an additional hour is added.

Please note that we stay in summer time(+2) until 3:00:00 and then the clock is set to 02:00:00 winter time(+1).

Test script:
---------------
$date = new DateTime(
	'2013-10-27 01:45:00',
	new DateTimeZone('Europe/Berlin')
);
$oldStamp=$date->getTimeStamp();
$interval=new DateInterval('PT15M');
echo $date->format('c')."\n";
echo $date->getTimeStamp()."\n";
$date->add($interval);
echo $date->format('c')."\n";
echo $date->getTimeStamp()."\n";
echo $date->getTimeStamp()-$oldStamp;


Expected result:
----------------
2013-10-27T01:45:00+02:00
1382831100
2013-10-27T02:00:00+01:00
1382835600
4500

Actual result:
--------------
2013-10-27T01:45:00+02:00
1382831100
2013-10-27T02:00:00+02:00
1382832000
900

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-19 12:31 UTC] pajoye@php.net
-Package: date_time +Package: Date/time related
 [2012-10-19 12:31 UTC] pajoye@php.net
Fix cat
 [2012-10-19 12:32 UTC] thomas dot mayer at 2bis10 dot de
-Package: Date/time related +Package: date_time
 [2012-10-19 12:32 UTC] thomas dot mayer at 2bis10 dot de
I switched Expected result and actual result.

Expected result:
--------------
2013-10-27T01:45:00+02:00
1382831100
2013-10-27T02:00:00+02:00
1382832000
900

Actual result:
----------------
2013-10-27T01:45:00+02:00
1382831100
2013-10-27T02:00:00+01:00
1382835600
4500
 [2012-10-19 12:33 UTC] pajoye@php.net
-Package: date_time +Package: Date/time related
 [2012-10-19 12:33 UTC] pajoye@php.net
Fix category, tak #2.
 [2013-05-03 10:30 UTC] rob dot norman at infinity-tracking dot com
I'm seeing similar behaviour across daylight boundaries with CentOS 5.9, PHP 
5.4.13

Test script:
------------
$tzLon      = new DateTimeZone( 'Europe/London' );
$tzUtc      = new DateTimeZone( 'UTC' );
$interval   = new DateInterval( 'PT24H' );

// Before change from GMT to BST.
$strDt      = '2013-04-31 12:00:00';

// Adding the interval in UTC behaves as expected.
$dt = new DateTime( $strDt, $tzUtc );
$dt->add( $interval );
echo $dt->format( 'Y-m-d H:i:s' ) . "\n";

// Adding the interval in Europe/London does not.
$dt = new DateTime( $strDt, $tzLon );
$dt->add( $interval );
// Convert to UTC for comparison.
$dt->setTimeZone( $tzUtc );
echo $dt->format( 'Y-m-d H:i:s' ) . "\n";


Expected result:
----------------
2013-05-02 12:00:00
2013-05-02 12:00:00


Actual result:
--------------
2013-05-02 12:00:00
2013-05-02 11:00:00
 [2013-07-03 15:01 UTC] mas at crosscan dot com
For me it seems not to be directly connected with switching from or to DST.

I tried the following
Test script:
----------------

date_default_timezone_set( 'Europe/Berlin' );
$date = new \DateTime( '@0' );

echo $date->format('c')."\n";

$interval = new \DateInterval( 'PT1H' );

$date->add($interval);
echo $date->format('c')."\n";

$date->add($interval);
echo $date->format('c')."\n";

Expected result:
------------------
1970-01-01T00:00:00+00:00
1970-01-01T01:00:00+00:00
1970-01-01T02:00:00+00:00

Actual result:
----------------
1970-01-01T00:00:00+00:00
1970-01-01T02:00:00+00:00
1970-01-01T04:00:00+00:00
 [2014-03-31 11:27 UTC] nj dot johansson at gmail dot com
Experiencing the same thing on PHP 5.4.26 (RHEL 5.6) when subtracting an hour at the time across DST boundary.

Test script:
---------------
// Init DateTime after DST change
$dt = new DateTime('2014-03-30 05:03:00', new DateTimeZone('Europe/Berlin')); 
$di = new DateInterval('PT1H');

// Subtract one hour each time and notice how time is stuck
echo $dt->format('Y-m-d H:i:s') . "\n";
$dt->sub($di);
echo $dt->format('Y-m-d H:i:s') . "\n";
$dt->sub($di);
echo $dt->format('Y-m-d H:i:s') . "\n";
$dt->sub($di);
echo $dt->format('Y-m-d H:i:s') . "\n";
$dt->sub($di);
echo $dt->format('Y-m-d H:i:s') . "\n";

Result:
-----------------
2014-03-30 05:03:00
2014-03-30 04:03:00
2014-03-30 03:03:00
2014-03-30 03:03:00
2014-03-30 03:03:00
2014-03-30 03:03:00
 [2014-03-31 11:37 UTC] nj dot johansson at gmail dot com
Seems like there are many similar reported bugs that might be related to the same underlying issue:
https://bugs.php.net/bug.php?id=51051
https://bugs.php.net/bug.php?id=60960
https://bugs.php.net/bug.php?id=61530
https://bugs.php.net/bug.php?id=62331
https://bugs.php.net/bug.php?id=62327
 [2014-11-11 08:21 UTC] YAfrourner5561 at superrito dot com
Bug still persists
 [2016-02-18 08:44 UTC] jan dot machala at email dot cz
Problem still persists in newest php versions. There is online php shell test, which detects which php versions are affected (https://3v4l.org/femQK).
 [2017-01-16 06:48 UTC] heiglandreas@php.net
-PHP Version: 5.4.8 +PHP Version: 7.1
 [2017-03-19 10:47 UTC] heiglandreas@php.net
-Status: Open +Status: Duplicate
 [2017-03-19 10:47 UTC] heiglandreas@php.net
We're now tracking this in https://bugs.php.net/bug.php?id=74274
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 11:01:30 2024 UTC