php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67269 Adding DateInterval to a DateTime object does not add the correct amount
Submitted: 2014-05-13 17:38 UTC Modified: 2017-03-19 11:29 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: kai dot xu at achievers dot com Assigned:
Status: Duplicate Package: Date/time related
PHP Version: 5.4.27 OS: Linux
Private report: No CVE-ID: None
 [2014-05-13 17:38 UTC] kai dot xu at achievers dot com
Description:
------------
When adding a DateInterval to a DateTime object, it adds unexpected amounts of seconds. 

For the following time frames, adding DateInterval doesn't work correctly (my PHP version 5.4.27)

// PT1M – adding 1 minute. Expected timestamp differential : 60 seconds

1414907940 - 1414911539 ( 2014-11-02 05:59:00 - 2014-11-02 06:58:59 )
test script result: -3540
1414911540 - 1414911599 ( 2014-11-02 06:59:00 - 2014-11-02 06:59:59 )
test script result: -7140
1446357540 - 1446361139 ( 2015-11-01 05:59:00 - 2015-11-01 06:58:59 )
test script result: -3540
1446361140 - 1446361199 ( 2015-11-01 06:59:00 - 2015-11-01 06:59:59 )
test script result: -7140

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

// PT1H -  adding 1 hour. Expected timestamp differential : 3600 seconds

1414904400 - 1414907999 ( 2014-11-02 05:00:00 - 2014-11-02 05:59:59 )
test script result: 0
1414908000 - 1414911599 ( 2014-11-02 06:00:00 - 2014-11-02 06:59:59 )
test script result: -3600
1446354000 - 1446357599 ( 2015-11-01 05:00:00 - 2015-11-01 05:59:59 )
test script result: 0
1446357600 - 1446361199 ( 2015-11-01 06:00:00 - 2015-11-01 06:59:59 )
test script result: -3600

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

// P1D - adding 1 day. Expected timestamp differential : 86400 seconds

1394265600 - 1394348399  ( 2014-03-08 08:00:00 - 2014-03-09 06:59:59 )
test script result: 82800
1414821600 - 1414907999 ( 2014-11-01 06:00:00 - 2014-11-02 05:59:59 )
test script result: 90000
1425715200 - 1425797999 ( 2015-03-07 08:00:00 - 2015-03-08 06:59:59 )
test script result: 82800
1446271200 - 1446357599 ( 2015-10-31 06:00:00 - 2015-11-01 05:59:59 )
test script result: 90000

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





Test script:
---------------
// e.g. adding 1 minute to a DateTime object. The difference should be 60 but it returns -3560.

$ts = 1414907940;
$d = date('Y-m-d  H:i:s', $ts);
$dt = new DateTime($d);
$di = new DateInterval('PT1M');
$dt->add($di);

echo ($dt->getTimestamp() - $ts);  // expecting 60 


Expected result:
----------------
add the correct minute/hour/day to a DateTime object for any DateTime object

Actual result:
--------------
For some time frames, it sometimes adds more, sometimes adds less number of seconds to a Datetime object. (see description)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-20 18:36 UTC] kai dot xu at achievers dot com
-PHP Version: 5.4.28 +PHP Version: 5.4.27
 [2014-05-20 18:36 UTC] kai dot xu at achievers dot com
The timezone setting is America/New_York
 [2014-05-24 15:25 UTC] sea128 at post dot cz
Caused by daylight saving.

Similar to https://bugs.php.net/bug.php?id=63442
 [2017-03-19 11:29 UTC] heiglandreas@php.net
-Status: Open +Status: Duplicate
 [2017-03-19 11:29 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: Sat Dec 21 12:01:31 2024 UTC