Bug #69378 [Opn->Ver]: Incorrect Result for DateTime::sub()
| From: | cmb@php.net | Date: | Thu, 21 Jan 2021 14:51:52 +0000 |
| Subject: | Bug #69378 [Opn->Ver]: Incorrect Result for DateTime::sub() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-231680@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69378&edit=1
ID: 69378
Updated by: cmb@php.net
Reported by: support at eggplantsd dot com
Summary: Incorrect Result for DateTime::sub()
-Status: Open
+Status: Verified
Type: Bug
Package: Date/time related
Operating System: Debian Wheezy x64
PHP Version: 5.4.39
Block user comment: N
Private report: N
New Comment:
Date/time arithmetic is inherently hard. In this case it is about
the order of the operations:
2014-04-02 - 17d = 2014-03-16
2014-03-16 - 1m = 2014-02-16
vs.
2014-04-02 - 1m = 2014-03-02
2014-03-02 - 17d = 2014-02-13
Previous Comments:
------------------------------------------------------------------------
[2015-04-14 07:06:50] andreas at heigl dot org
The change seems to have been introduces in PHP5.3.3 as can be seen in http://3v4l.org/mUeRk.
------------------------------------------------------------------------
[2015-04-05 14:47:09] support at eggplantsd dot com
Description:
------------
B - (B - A) == A SHOULD HOLD TRUE
DOES HOLD TRUE USING UNIX TIMESTAMP MATH
DOES NOT HOLD TRUE USING DateTime:sub()
Test script:
---------------
/*
METHODOLOGY:
B - (B - A) == A SHOULD HOLD TRUE
DOES HOLD TRUE USING UNIX TIMESTAMP MATH
DOES NOT HOLD TRUE USING DateTime:sub()
*/
$DF_MYSQL = 'Y-m-d H:i:s';
$dtB = DateTime::createFromFormat($DF_MYSQL, '2015-04-02 09:44:47');
$dtA = DateTime::createFromFormat($DF_MYSQL, '2014-02-16 02:00:00');
// DateInterval: B-A
$diDiff = $dtA->diff($dtB);
// Unix Timestamp: B-A
$tsDiff = $dtB->getTimestamp() - $dtA->getTimestamp();
// REPORT DIFFERENCES
/*
NOTE: DIFFERENCE B-A IS
$y => 1
$m => 1
$d => 17
$h => 7
$i => 44
$s => 47
*/
print_r($diDiff);
// Unix Timestamp: CALCULATE A FROM (B - DIFFERENCE) AND REPORT
// NOTE: CORRECT RESULT, SAME AS $dtA: 2014-02-16 02:00:00
$tsShift = $dtB->getTimestamp() - $tsDiff;
$dtOut = new DateTime();
$dtOut->setTimestamp($tsShift);
print_r($dtOut);
// DateInterval: CALCULATE A FROM (B - DIFFERENCE) AND REPORT
// NOTE: INCORRECT RESULT, DIFFERENT FROM $dtA: 2014-02-13 02:00:00
$dtB->sub($diDiff);
print_r($dtB);
Expected result:
----------------
SEE SCRIPT COMMENTS
Actual result:
--------------
SEE SCRIPT COMMENTS
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69378&edit=1