php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68078 Datetime comparisons ignore microseconds
Submitted: 2014-09-22 19:50 UTC Modified: 2016-01-29 14:33 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: stephen dot r dot burrows at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: Irrelevant OS: OS X 10.9.4
Private report: No CVE-ID: None
 [2014-09-22 19:50 UTC] stephen dot r dot burrows at gmail dot com
Description:
------------
Two datetimes with all values the same (except microseconds) always compare as "equal". To put it another way, if you're trying to find out which one came first, you can't.

Test script:
---------------
$dt1 = new DateTime("2014-09-19 17:16:35.694274 America/Los_Angeles");
$dt2 = new DateTime("2014-09-19 17:16:35.728291 America/Los_Angeles");
var_dump($dt1 < $dt2);
# bool(false)
var_dump($dt1 == $dt2);
# bool(true)

# Not setting timezone because already have same timezone.
var_dump($dt1->format("Y-m-d H:i:s.u") < $dt2->format("Y-m-d H:i:s.u"));
# bool(true)
var_dump($dt1->format("Y-m-d H:i:s.u") == $dt2->format("Y-m-d H:i:s.u"));
# bool(false)

Expected result:
----------------
The smaller datetime should compare as less than the larger one. To put this another way, the comparison of the objects should be the same whether you're comparing them as objects or as strings (and the string version is clearly correct IMO.)


Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-29 14:33 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2016-01-29 14:33 UTC] derick@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Thanks Willem-Jan!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC