php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74529 DateTime::diff seems to ignore $absolute = true
Submitted: 2017-05-02 11:03 UTC Modified: 2017-05-14 04:22 UTC
From: hrovira at seocom dot es Assigned:
Status: No Feedback Package: Date/time related
PHP Version: 7.1.4 OS: Ubuntu 16.04.2 LTS
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2017-05-02 11:03 UTC] hrovira at seocom dot es
Description:
------------
DateTime::diff generates different DateInterval results depending on the order, even though $absolute param is set to true. date1->diff(date2, true) is different than date2->diff(date1, true). Please, see test script.

Test script:
---------------
<?php

$date1 = date_create("2014-11-01 00:00:00");
$date2 = date_create("2017-05-01 04:00:00");

$diff1 = $date2->diff($date1, true);
$diff2 = $date1->diff($date2, true);

echo $diff1->m .' months, '.$diff1->d. ' days';
// outputs: 6 months, 0 days
echo $diff2->m .' months, '.$diff2->d. ' days';
// outputs: 5 months, 30 days


Expected result:
----------------
6 months, 0 days
5 months, 30 days

Actual result:
--------------
6 months, 0 days
6 months, 0 days

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-02 11:04 UTC] hrovira at seocom dot es
Obviously, expected and actual results are switched :(
 [2017-05-02 12:12 UTC] andrew dot nester dot dev at gmail dot com
thanks for reporting this issue!
I tried to reproduce it on latest 7.1 build (7.1.6-dev) - working good.
but anyway I decided to add test to prove that it works as expected (see my PR)
 [2017-05-02 12:34 UTC] derick@php.net
Andrew, it might look fixed for you because you have a different timezone with different DST rules configured.
 [2017-05-02 14:10 UTC] cmb@php.net
-Package: SPL related +Package: Date/time related
 [2017-05-02 20:06 UTC] andrew dot nester dot dev at gmail dot com
Thanks Derick!
gonna rework my pull request
 [2017-05-03 12:17 UTC] andrew dot nester dot dev at gmail dot com
I've just updated my PR to fix the issue.
See https://github.com/php/php-src/pull/2509
Thanks!
 [2017-05-06 18:56 UTC] heiglandreas@php.net
-Status: Open +Status: Feedback
 [2017-05-06 18:56 UTC] heiglandreas@php.net
To me this looks like a duplicate of https://bugs.php.net/bug.php?id=52480 depending on which way the diff starts…
 [2017-05-14 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Oct 19 16:03:35 2021 UTC