php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #67034 Unable to compare DateInterval instances with == operator
Submitted: 2014-04-06 12:16 UTC Modified: 2022-05-20 16:11 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: from dot php dot net at brainbox dot cz Assigned: cmb (profile)
Status: Closed Package: Date/time related
PHP Version: 5.6.0alpha3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: from dot php dot net at brainbox dot cz
New email:
PHP Version: OS:

 

 [2014-04-06 12:16 UTC] 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)

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-08 17:30 UTC] 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)
 [2021-02-26 21:50 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2021-02-26 21:50 UTC] 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.
 [2022-05-13 14:18 UTC] derick@php.net
Where would we document this CMB?
 [2022-05-20 12:29 UTC] derick@php.net
-Assigned To: +Assigned To: cmb
 [2022-05-20 16:11 UTC] cmb@php.net
-Summary: Unable to compare DatePeriod instances with == operator +Summary: Unable to compare DateInterval instances with == operator
 [2022-05-20 16:12 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix bug 67034: Unable to compare DateInterval instances with == operator
On GitHub:  https://github.com/php/doc-en/pull/1590
Patch:      https://github.com/php/doc-en/pull/1590.patch
 [2022-05-23 09:31 UTC] git@php.net
Automatic comment on behalf of cmb69 (author) and web-flow (committer)
Revision: https://github.com/php/doc-en/commit/3d9dad29c75a70e7985ed595dfa2a836e61418da
Log: Fix bug #67034: Unable to compare DateInterval instances with == operator
 [2022-05-23 09:31 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Sep 26 06:05:51 2022 UTC