|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70381 Support comparing DateInterval objects
Submitted: 2015-08-28 10:48 UTC Modified: 2017-03-24 06:37 UTC
From: accido at ukr dot net Assigned:
Status: Duplicate Package: Date/time related
PHP Version: 5.6.13RC1 OS: Xubuntu 14
Private report: No CVE-ID: None
 [2015-08-28 10:48 UTC] accido at ukr dot net
Wrong DateInterval comparsion result

Test script:
$a = new DateTime('1981-10-10');
$b = new DateTime('1980-10-01');
$m = new DateInterval('P1M');
var_dump($a->diff($b) > $m);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-28 11:45 UTC]
-Summary: DateInterval comparsion wrong +Summary: Support comparing DateInterval objects -Type: Bug +Type: Feature/Change Request
 [2015-08-28 11:45 UTC]
DateIntervals are not comparable. You can use ->days as a workaround.
 [2015-09-10 13:54 UTC] john_zuk at wp dot pl
You can't use ->days as a workaround, because of:
 [2015-09-10 19:52 UTC]
Ha, that's true. And it makes sense since it's not always possible to determine the number of days in an arbitrary interval. That could be a problem when trying to compare two DateIntervals in a general sense, though: what is P30D <=> P1M?

A better approach here would be to compare the start date + interval <=> end date.
 [2016-05-16 12:11 UTC] php dot net at anthonychambers dot co dot uk
I actually see this working back to 5.5.8 and up to 7.0.6 according to

The trick is that you have to inspect both intervals first. I just essentially did this:

var_export($interval1, true);
var_export($interval2, true);
var_export($interval1 < $interval2);

And that should work. If you don't check either of the intervals then you get different results:

* Check 0 = Always returns false
* Check 1 = Always returns true
* Check 2 = Returns correct values
 [2017-03-24 06:37 UTC]
-Status: Open +Status: Duplicate
 [2017-03-24 06:37 UTC]
I just became aware of request #49914.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Aug 16 19:03:35 2022 UTC