Doc #77202 [Com]: DateInterval compare unpredictable result

From: Date: Mon, 26 Nov 2018 11:23:37 +0000
Subject: Doc #77202 [Com]: DateInterval compare unpredictable result
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-16175@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77202&edit=1 ID: 77202 Comment by: a at b dot c dot de Reported by: thomas dot wanka at gmx dot net Summary: DateInterval compare unpredictable result Status: Open Type: Documentation Problem Package: Date/time related Operating System: Linux PHP Version: 7.2.12 Block user comment: N Private report: N New Comment: 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. <?php $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"; print_r($actualDiff); echo "\n"; echo "Weekday: ", $actualDiff->weekday; Previous Comments: ------------------------------------------------------------------------ [2018-11-26 08:53:54] thomas dot wanka at gmx dot net Description: ------------ 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: --------------- <?php $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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77202&edit=1

« previous php.doc.bugs (#16175) next »