Doc #67034 [Ver]: Unable to compare DatePeriod instances with == operator
| From: | derick@php.net | Date: | Fri, 13 May 2022 14:18:24 +0000 |
| Subject: | Doc #67034 [Ver]: Unable to compare DatePeriod instances with == operator | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-19431@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67034&edit=1
ID: 67034
Updated by: derick@php.net
Reported by: from dot php dot net at brainbox dot cz
Summary: Unable to compare DatePeriod instances with ==
operator
Status: Verified
Type: Documentation Problem
Package: Date/time related
PHP Version: 5.6.0alpha3
Block user comment: N
Private report: N
New Comment:
Where would we document this CMB?
Previous Comments:
------------------------------------------------------------------------
[2021-02-26 21:50:51] cmb@php.net
DatePeriod comparision are explicitly disallowed (and as such
return false) as of PHP 7.4.0, for the reasons outlined above.
This is apparently not yet documented, though.
------------------------------------------------------------------------
[2014-12-08 17:30:47] mfaust at usinternet dot com
Comparisons of DatePeriod really ins't feasible without knowing a start date. For example
'P1M' and 'P30D' are equal (==) for some months, but not others. The ==
comparison is possible for time-only date periods but for anything involving dates you can't
make a reliable assumption. My recommendation is to return TRUE when all the parts patch exactly (eg
'PT60S' == 'PT60S' but 'PT1M' != 'PT60S') just to make the
behavior consistent if a date portion is involved.
If you want a true comparison then do something like this:
$now = new DateTimeImmutable();
if($now->addInterval($intervalA) == $now->addInterval($intervalB)){...
(note that DateTimeImmutable will return a new object for each instead of modifying $now)
------------------------------------------------------------------------
[2014-04-06 12:16:53] from dot php dot net at brainbox dot cz
Description:
------------
I am unable to compare DatePeriod timezones with equality operator. The operator always returns true
regardless the DatePeriod value.
Result can be seen here: http://3v4l.org/b8AAS
Test script:
---------------
// different
var_dump(new DateInterval('PT1S') == new DateInterval('PT2S'));
// same
var_dump(new DateInterval('PT1S') == new DateInterval('PT1S'));
Expected result:
----------------
bool(false)
bool(true)
Actual result:
--------------
bool(true)
bool(true)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67034&edit=1