php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62331 DateTime::add() produces inconsistent resultst
Submitted: 2012-06-15 08:46 UTC Modified: 2017-03-19 10:34 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:3 (100.0%)
From: ruesche at fka dot de Assigned: derick (profile)
Status: Duplicate Package: Date/time related
PHP Version: Irrelevant OS: Windows, Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ruesche at fka dot de
New email:
PHP Version: OS:

 

 [2012-06-15 08:46 UTC] ruesche at fka dot de
Description:
------------
When you use a timezone with daylight saving time (like Europe/Berlin) and the date of the system running the PHP script is in this daylight saving time (like 2012-06-15), DateTime::add() will produce results that are off by 1 hour.

When the date on the local machine is not in the daylight saving time, the same computation will yield the expceted results.


This bug also only appears, when the DateTime instance was created using a timestamp, so a workaround would be the following: Instead of

  $date = new DateTime(gmmktime(0, 0, 0, 1, 1, 2012));

use

  $date = new DateTime('2012-01-01');

The script was tested with the following systems, all having the same problem:
- Windows XP, PHP 5.3.10
- Windows XP, PHP 5.3.13
- Windows 7, PHP 5.3.10
- Arch Linux (x86_64), PHP 5.4.3
- Debian Squeeze, PHP 5.3.10

Test script:
---------------
<?php
$oneMonth = new DateInterval('P1M');

$date = new DateTime('@'.gmmktime(0, 0, 0, 1, 1, 2012));
echo $date->format('Y-m-d H:i:s')."\n";

$date->add($oneMonth);
echo $date->format('Y-m-d H:i:s')."\n";

Expected result:
----------------
2012-01-01 00:00:00
2012-02-01 00:00:00

Actual result:
--------------
2012-01-01 00:00:00
2012-02-01 01:00:00

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-15 08:48 UTC] ruesche at fka dot de
-Summary: DateTime::add() produces inconsisten results +Summary: DateTime::add() produces inconsistent resultst
 [2012-06-15 08:48 UTC] ruesche at fka dot de
typo fixed.
 [2012-06-15 09:02 UTC] salathe@php.net
-Assigned To: +Assigned To: derick
 [2012-06-15 09:02 UTC] salathe@php.net
This looks like a side-effect of the changes made against bug #55253.
 [2013-03-20 20:38 UTC] kavi at postpro dot net
This is also affecting DateTime->modify() in PHP 5.3.21.

This appears to only affect DateTime objects with timezone_type of 1, and maybe 2.

Of course, it's impossible to directly query a DateTime object's timezone_type, so you have to get the string representation from print_r and inspect that in order to code around this bug.

Unbelievable.
 [2013-05-03 10:50 UTC] rob dot norman at infinity-tracking dot com
Looks like this has been duplicated a few times.

https://bugs.php.net/bug.php?id=61530
https://bugs.php.net/bug.php?id=63953
https://bugs.php.net/bug.php?id=63311

I just added a comment & test case to the last one, still seeing this on PHP 
5.4.13
 [2017-03-19 10:34 UTC] heiglandreas@php.net
-Status: Assigned +Status: Duplicate
 [2017-03-19 10:34 UTC] heiglandreas@php.net
As this has already been reported as duplicate we'll track it at https://bugs.php.net/bug.php?id=74274
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC