php.net |  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
Votes:61
Avg. Score:4.6 ± 0.7
Reproduced:57 of 57 (100.0%)
Same Version:22 (38.6%)
Same OS:43 (75.4%)
From: aharvey@php.net 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] aharvey@php.net
Description:
------------
(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 http://www.adamharvey.name/patches/DateInterval-comparators.patch 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:
---------------
<?php
$ten = new DateInterval('PT10S');
$twenty = new DateInterval('PT20S');
var_dump($ten < $twenty);
?>

Expected result:
----------------
bool(true)

Actual result:
--------------
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-24 10:50 UTC] jani@php.net
-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.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 22:02:05 2014 UTC