php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70126 Wrong calculation of the difference between two dates
Submitted: 2015-07-24 09:50 UTC Modified: 2015-07-24 10:33 UTC
From: php at tul dot io Assigned: derick (profile)
Status: Suspended Package: Date/time related
PHP Version: 7.0.0beta2 OS: any
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: php at tul dot io
New email:
PHP Version: OS:

 

 [2015-07-24 09:50 UTC] php at tul dot io
Description:
------------
DateTime::diff() reports the wrong number of days and months when not in an inverted order (start date < end date). To reproduce, the two dates used as input have to be even number of months apart and the day of the start date has to be greater than the day of the end date (e.g. 2015-01-31 to 2015-03-01).

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

$date1 = new DateTime('2015/01/31');
$date2 = new DateTime('2015/03/01');

print_r($date1->diff($date2));
print_r($date2->diff($date1));

Expected result:
----------------
DateInterval Object
(
    [y] => 0
    [m] => 1
    [d] => 1
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 29
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
DateInterval Object
(
    [y] => 0
    [m] => 1
    [d] => 1
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 1
    [days] => 29
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

Actual result:
--------------
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 29
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 29
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)
DateInterval Object
(
    [y] => 0
    [m] => 1
    [d] => 1
    [h] => 0
    [i] => 0
    [s] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 1
    [days] => 29
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

Patches

bug-datetime-diff-calculations (last revision 2015-07-24 09:51 UTC) by php at tul dot io)

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-24 10:33 UTC] derick@php.net
-Status: Open +Status: Suspended -Assigned To: +Assigned To: derick
 [2015-07-24 10:33 UTC] derick@php.net
timelib development lives at http://github.com/derickr/timelib - the one in PHP is a bundled version. Timelib does right now not have a test harnass for diffs, although there is a tester for it: https://github.com/derickr/timelib/blob/master/tests/tester-diff.c

Once it's fixed (and tested) in timelib, I'm happy to resync it back to PHP.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC