|  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
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
14 + 50 = ?
Subscribe to this entry?

 [2014-09-22 19:50 UTC] stephen dot r dot burrows at gmail dot com
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.)


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-29 14:33 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2016-01-29 14:33 UTC]
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

 For Windows:
Thank you for the report, and for helping us make PHP better.

Thanks Willem-Jan!
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Oct 04 06:05:54 2022 UTC