PHP :: Bug #68078 :: Datetime comparisons ignore microseconds
| Bug #68078 | Datetime comparisons ignore microseconds | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Submitted: | 2014-09-22 19:50 UTC | Modified: | 2016-01-29 14:33 UTC |
|
||||||||||
| 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
Pull Requests
History
AllCommentsChangesGit/SVN commits
[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