Bug #77854 [Com]: DateTime object comparison wrong

From: Date: Mon, 08 Apr 2019 07:42:43 +0000
Subject: Bug #77854 [Com]: DateTime object comparison wrong
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220354@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77854&edit=1 ID: 77854 Comment by: Stephan dot Hartmann at flyeralarm dot com Reported by: Stephan dot Hartmann at flyeralarm dot com Summary: DateTime object comparison wrong Status: Feedback Type: Bug Package: Date/time related Operating System: Ubuntu PHP Version: 7.1.28 Block user comment: N Private report: N New Comment: After compiling 7.1.27 locally on my machine (Ubuntu 18.04.02) this minimal example passes, while on the current "php:7.1-cli-alpine" docker image from docker hub this minimal example fails. Please try to reproduce bug using said image. Additionally using $date = new DateTime('2019-04-05 12:34:56.123456'); passes both times, while using $date = new DateTime(); fails in docker image; Previous Comments: ------------------------------------------------------------------------ [2019-04-05 16:37:51] requinix@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. Using the current time is not repeatable. Please pick a specific time. PHP 7.1 is no longer in active support, but I can't reproduce this behavior with it anyways. https://3v4l.org/3vaor ------------------------------------------------------------------------ [2019-04-05 11:53:24] Stephan dot Hartmann at flyeralarm dot com Description: ------------ Only in php7.1 relevant, php5.6 and php7.2 not Comparing two DateTime objects fails even if they are identical. Problem is probably float arithmetic because $date->diff($date1) != 0. Test script: --------------- <?php $date = new DateTime(); $date1 = unserialize(serialize($date)); $date2 = new DateTime($date->format('Y-m-d H:i:s.u')); print_r("start php version ".phpversion()); print_r("\n"); print_r("\n"); print_r("date==date1:"); print_r($date==$date1); print_r("\n"); print_r("date==date2:"); print_r($date==$date2); print_r("\n"); print_r("date1==date2:"); print_r($date1==$date2); print_r("\n"); print_r("finish"); Expected result: ---------------- start php version 7.2.15-0ubuntu0.18.04.2 date==date1:1 date==date2:1 date1==date2:1 finish Process finished with exit code 0 Actual result: -------------- start php version 7.2.15-0ubuntu0.18.04.2 date==date1: date==date2: date1==date2:1 finish Process finished with exit code 0 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=77854&edit=1

« previous php.bugs (#220354) next »