Last active
May 21, 2017 17:41
-
-
Save frankdejonge/fff3cdc43c6e7bf3fe83ba4d7b4b2c51 to your computer and use it in GitHub Desktop.
Mirco-time precise DateTime(Immutable) instances.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mirco_time_precise_date_time_immutable() | |
{ | |
return DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true))); | |
} |
The 'rounding errors' are caused by sprintf("%06d", ($microTime - $timestamp) * 1000000)
which will cast to an integer, dropping obsolete digits instead of rounding the value. Doubt that that would be significant to anybody, though ;)
Note that the time zone will not be set to the current time zone, it will default to "+00:00". This is a side-effect from creating a DateTime(Immutable) from a timestamp.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Via https://twitter.com/tacovdb/status/777762473218801664:
Seems to be the shorter variant that produces the same results (except from some rounding errors that might come from the additional
date()
call):https://3v4l.org/cQtmk