php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77854 DateTime object comparison wrong
Submitted: 2019-04-05 11:53 UTC Modified: 2019-04-11 07:40 UTC
From: Stephan dot Hartmann at flyeralarm dot com Assigned: cmb (profile)
Status: Not a bug Package: Date/time related
PHP Version: 7.1.28 OS: Ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: Stephan dot Hartmann at flyeralarm dot com
New email:
PHP Version: OS:

 

 [2019-04-05 11:53 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-05 16:37 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2019-04-05 16:37 UTC] 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-08 07:42 UTC] Stephan dot Hartmann at flyeralarm dot com
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;
 [2019-04-08 08:00 UTC] Stephan dot Hartmann at flyeralarm dot com
More precisely its the 4 week old PHP 7.1.27 (cli) (built: Mar 0 2019 03:44:52)
php 7.1-cli-alpine e6492c17926d 4 weeks ago 70MB
 [2019-04-08 14:09 UTC] Stephan dot Hartmann at flyeralarm dot com
Using the php:7.1.27-cli-stretch image from dockerhub passes the testscript.
Using the php:7.1.28-cli-alpine image from dockerhub fails the testscript, hinting that the whole php:7.1.*-cli-alpine images are affected.
 [2019-04-08 15:40 UTC] requinix@php.net
-Status: Feedback +Status: Open
 [2019-04-10 12:35 UTC] Stephan dot Hartmann at flyeralarm dot com
https://github.com/Talarion291/phpalpinebug

check this out for minimal example
 [2019-04-10 21:54 UTC] derick@php.net
(built: Mar 0 2019 03:44:52)

Mar 0? That's... peculiar.
 [2019-04-11 06:26 UTC] Stephan dot Hartmann at flyeralarm dot com
@derick@php.net sry, typo: should read Mar 09
 [2019-04-11 07:40 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-04-11 07:40 UTC] cmb@php.net
Thank you for taking the time to report a problem with PHP.
Unfortunately you are not using a current version of PHP -- 
the problem might already be fixed. Please download a new
PHP version from http://www.php.net/downloads.php

If you are able to reproduce the bug with one of the latest
versions of PHP, please change the PHP version on this bug report
to the version you tested and change the status back to "Open".
Again, thank you for your continued support of PHP.

PHP 7.1 receives only security fixes.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 20:01:29 2024 UTC