Bug #69378 [Ver->Nab]: Incorrect Result for DateTime::sub()
Edit report at https://bugs.php.net/bug.php?id=69378&edit=1
ID: 69378
Updated by: derick@php.net
Reported by: support at eggplantsd dot com
Summary: Incorrect Result for DateTime::sub()
-Status: Verified
+Status: Not a bug
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:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
DateTime::diff documentation:
"The return value more specifically represents the interval to apply to the original object
($this or $originObject) to arrive at the $targetObject. This process is not always reversible.
"
As @cmb already explained, that is the case here.
Previous Comments:
------------------------------------------------------------------------
[2021-01-21 14:51:52] cmb@php.net
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
------------------------------------------------------------------------
[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
Thread (4 messages)