|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-10-29 09:50 UTC] 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) { } PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 03:00:01 2025 UTC |
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)