From:             ahar...@php.net
Operating system: Irrelevant
PHP version:      5.3SVN-2009-10-18 (SVN)
PHP Bug Type:     Date/time related
Bug description:  DateInterval doesn't implement comparison functions

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)

-- 
Edit bug report at http://bugs.php.net/?id=49914&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49914&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49914&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49914&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49914&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49914&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49914&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49914&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49914&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49914&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49914&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49914&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49914&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49914&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49914&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49914&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49914&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49914&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49914&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49914&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49914&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49914&r=mysqlcfg

Reply via email to