|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77202 DateInterval compare unpredictable result
Submitted: 2018-11-26 08:53 UTC Modified: 2022-06-04 17:16 UTC
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: thomas dot wanka at gmx dot net Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.2.12 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
45 + 35 = ?
Subscribe to this entry?

 [2018-11-26 08:53 UTC] thomas dot wanka at gmx dot net
The result if compare operator on two DateInterval objects is unpredictable.
See code in the "test script" section:
the result of the if() statement depends on the code before. If, for instance, you print the DateInterval, the internal structure seems to change, and the result is correct. Otherwise, the result is not correct. The system should output an error or a warning if the compare operater is not fully implemented.

Test script:
$old = DateTime::createFromFormat('Y-m-d H:i:s', '2018-11-26 09:29:00'); 
// constant here for demo only, i read datetime field from database
$now = new DateTime('now',new DateTimeZone('Europe/Berlin'));
$maxdiff = new DateInterval('PT30M');
$actualDiff = $now->diff($old);

// echo "<pre>"; print_r($now); echo "</pre><br />";
// echo "<pre>"; print_r($maxdiff ); echo "</pre><br />";

if ($actualDiff>$maxDiff) echo "time is up" else echo "just wait";

Expected result:
throw an error if an nvalid operator ist used 
minimum request: add information about this behauviour into documentation of DateInterval

Actual result:
operator is performed, result is unpredictable


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-11-26 11:23 UTC] a at b dot c dot de
Where did you read that > could be used to compare objects in the first place?

What does happen is that if you do a debugging dump of a DateTime object it creates a bunch of additional properties.
$old = DateTime::createFromFormat('Y-m-d H:i:s', '2018-11-26 09:29:00'); 
$now = new DateTime('now',new DateTimeZone('Europe/Berlin'));
$actualDiff = $now->diff($old);

echo "Weekday: ", $actualDiff->weekday;
echo "\n";
echo "\n";
echo "Weekday: ", $actualDiff->weekday;
 [2018-12-04 23:04 UTC] a at b dot c dot de
See also Bug #74940
 [2022-06-04 17:16 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2022-06-04 17:16 UTC]
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at

This was addressed in PHP 7.4, where the script (After fixing syntax), now shows:

Warning: Cannot compare DateInterval objects in Standard input code on line 11
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 05 15:01:31 2024 UTC