php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69378 Incorrect Result for DateTime::sub()
Submitted: 2015-04-05 14:47 UTC Modified: -
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: support at eggplantsd dot com Assigned:
Status: Open Package: Date/time related
PHP Version: 5.4.39 OS: Debian Wheezy x64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: support at eggplantsd dot com
New email:
PHP Version: OS:

 

 [2015-04-05 14:47 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-14 07:06 UTC] 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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Sep 28 11:01:23 2020 UTC