Req #70381 [Opn->Dup]: Support comparing DateInterval objects

[email protected]
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=70381&edit=1

 ID:                 70381
 Updated by:         [email protected]
 Reported by:        accido at ukr dot net
 Summary:            Support comparing DateInterval objects
-Status:             Open
+Status:             Duplicate
 Type:               Feature/Change Request
 Package:            Date/time related
 Operating System:   Xubuntu 14
 PHP Version:        5.6.13RC1
 Block user comment: N
 Private report:     N

 New Comment:

I just became aware of request #49914.


Previous Comments:
------------------------------------------------------------------------
[2016-05-16 12:11:50] 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 https://3v4l.org/TrPvp

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

------------------------------------------------------------------------
[2015-09-10 19:52:40] [email protected]

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.

------------------------------------------------------------------------
[2015-09-10 13:54:16] john_zuk at wp dot pl

You can't use ->days as a workaround, because of:
http://php.net/manual/en/class.dateinterval.php#dateinterval.props.days
See: https://3v4l.org/nlH2v

------------------------------------------------------------------------
[2015-08-28 11:45:43] [email protected]

DateIntervals are not comparable. You can use ->days as a workaround.

https://3v4l.org/H7JsN

------------------------------------------------------------------------
[2015-08-28 10:48:17] accido at ukr dot net

Description:
------------
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);
//bool(false)




------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=70381&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.