Bug #70810 [Asn->Csd]: DateInterval equals every other DateInterval

From: Date: Tue, 10 Mar 2020 10:21:59 +0000
Subject: Bug #70810 [Asn->Csd]: DateInterval equals every other DateInterval
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225995@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70810&edit=1 ID: 70810 Updated by: cmb@php.net Reported by: f dot bosch at genkgo dot nl Summary: DateInterval equals every other DateInterval -Status: Assigned +Status: Closed Type: Bug Package: Date/time related Operating System: linux PHP Version: 5.6.14 Assigned To: nikic Block user comment: N Private report: N New Comment: This is fixed as of PHP 7.4.0[1], so I'm closing this ticket. [1] <https://3v4l.org/bQnf0> Previous Comments: ------------------------------------------------------------------------ [2019-04-18 13:49:56] nikic@php.net https://github.com/php/php-src/pull/4039 ------------------------------------------------------------------------ [2016-12-15 02:59:52] rupert dot chen at gmail dot com I believe I have run into the same issue. It appears a new DateInterval is considered "0", or perhaps not fully initialized internally. After "certain interactions" with the DateInterval, for example as an arg to print_r(), it appears to become fully initialized. The provided example yields strange behavior in the two version of PHP at my disposal: PHP 5.4.45 (cli) (built: Sep 4 2015 15:40:44) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans PHP 5.5.36 (cli) (built: May 29 2016 01:07:06) Copyright (c) 1997-2015 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies Test Script ----------- <?php $x = new DateTime('2016-01-01 00:00:00 UTC'); $y = new DateTime('2016-01-01 00:00:15 UTC'); // Should be 0s $z1 = $x->diff($x); // Should be 15s $z2 = $x->diff($y); // Should be 30s $z3 = new DateInterval('PT30S'); echo "No inspections\n"; var_dump($z1 == $z2); var_dump($z1 == $z3); var_dump($z2 == $z3); var_dump($z1 > $z2); var_dump($z2 > $z3); echo "Inspect \$z1\n"; print_r($z1); var_dump($z1 == $z2); var_dump($z1 == $z3); var_dump($z2 == $z3); var_dump($z1 > $z2); var_dump($z2 > $z3); echo "Inspect \$z2\n"; print_r($z2); var_dump($z1 == $z2); var_dump($z1 == $z3); var_dump($z2 == $z3); // After inspecting all DateIntervals, these are the expected results. echo "Inspect \$z3\n"; print_r($z3); var_dump($z1 == $z2); var_dump($z1 == $z3); var_dump($z2 == $z3); var_dump($z1 > $z2); var_dump($z2 > $z3); Script Output ------------- No inspections bool(true) bool(true) bool(true) bool(false) bool(false) Inspect $z1 DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 0 [s] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 ) bool(false) bool(false) bool(true) bool(true) bool(false) Inspect $z2 DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 0 [s] => 15 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 0 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 ) bool(false) bool(false) bool(false) bool(false) bool(true) Inspect $z3 DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 0 [i] => 0 [s] => 30 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 ) bool(false) bool(false) bool(false) bool(false) bool(false) ------------------------------------------------------------------------ [2016-05-16 12:07:38] php dot net at anthonychambers dot co dot uk I've found a similar issue: https://3v4l.org/TrPvp What I found was that if I compared two DateInterval objects, they would always return false. That is, until I actually get some data from one of the objects (in my case I just var_export($interval, true) and discard the output) at which time everything starts to return true. Also weird. If I inspect both intervals first however, suddenly it all evaluates correctly! I've tested this in PHP 7.0.0 and PHP 7.0.6. 3v4l.org suggests that it will work back to 5.5.8 though ------------------------------------------------------------------------ [2015-10-29 09:52:41] f dot bosch at genkgo dot nl Actual and expected result are switched (sorry for that). ------------------------------------------------------------------------ [2015-10-29 09:50:59] f dot bosch at genkgo dot nl Description: ------------ DateInterval always equals another DateInterval object while their inner values are different. This is not expected behaviour. Looks like this is the case for all PHP versions: https://3v4l.org/bQnf0. If you ask me, this bug is different then #70381 (Support comparing DateInterval objects). Test script: --------------- <?php var_dump(new DateInterval('PT1H') == new DateInterval('P1D')); var_dump(new DateInterval('PT1H') === new DateInterval('P1D')); var_dump(new DateInterval('PT1H') , new DateInterval('P1D')); Expected result: ---------------- bool(true) bool(false) object(DateInterval)#3 (0) { } object(DateInterval)#4 (0) { } Actual result: -------------- bool(false) bool(false) object(DateInterval)#3 (0) { } object(DateInterval)#4 (0) { } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70810&edit=1

« previous php.bugs (#225995) next »