|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49914 DateInterval doesn't implement comparison functions
Submitted: 2009-10-18 17:20 UTC Modified: 2010-11-24 10:50 UTC
Avg. Score:4.6 ± 0.8
Reproduced:81 of 82 (98.8%)
Same Version:26 (32.1%)
Same OS:55 (67.9%)
From: Assigned:
Status: Open Package: Date/time related
PHP Version: 5.3SVN-2009-10-18 (SVN) OS: *
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-10-18 17:20 UTC]
(This is really a feature request, rather than a bug per se.)

Unlike DateTime objects, DateInterval objects cannot easily be compared within PHP. While it would be possible to concoct a workaround in userspace using a few calls to DateInterval::format() and some arithmetic, it would probably be preferable to implement it within ext/date itself.

I've prepared a patch (yes, it even has a simple test) against PHP_5_3 at which implements comparator support. I can probably prepare a HEAD patch if necessary; I just don't have a HEAD checkout to hand to do so at present.

There's one fairly significant issue with this patch worth noting: I've implemented a new function (timelib_rel_time_to_seconds) which converts a timelib_rel_time structure to the number of seconds it represents. The issue with this is that, as per bug #49778, we don't always know exactly how many days a timelib_rel_time actually represents because of the varying number of days in a month and year.

As per the comments in the function, for now I've fudged it and effectively chosen the default length of a month and year out of thin air if the days field isn't set within the structure. If the resolution to bug #49778 results in the days field always being filled in, then timelib_rel_time_to_seconds can be simplified accordingly. Alternatively, we could only support this in cases where we definitely know the days within the timelib_rel_time structure and error out otherwise.

As a side note, I have a second patch that I can upload that implements support for a DateInterval::getSeconds() method which effectively provides a PHP wrapper around the proposed internal timelib_rel_time_to_seconds function. If it's decided to accept this approach, I can create another bug to track getting that in.

Reproduce code:
$ten = new DateInterval('PT10S');
$twenty = new DateInterval('PT20S');
var_dump($ten < $twenty);

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-24 10:50 UTC]
-Package: Feature/Change Request +Package: Date/time related
 [2011-11-25 11:01 UTC] albertcasademont at gmail dot com
Did this patch make it into the trunk?
 [2012-03-23 00:35 UTC] kavi at postpro dot net
So... patch submitted 2.5 years ago.

Sup guys?
 [2012-09-26 15:25 UTC] jrbeaure at uvm dot edu
I need to be able to compare date intervals.
 [2015-12-03 14:28 UTC] will dot tinsdeall at mercianlabels dot com
Any closer to applying a fix on this? It's a real shame it didn't make it into 7.0
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Fri Mar 24 00:01:38 2017 UTC