Skip to content

Instantly share code, notes, and snippets.

@frankdejonge
Last active May 21, 2017 17:41
Show Gist options
  • Save frankdejonge/fff3cdc43c6e7bf3fe83ba4d7b4b2c51 to your computer and use it in GitHub Desktop.
Save frankdejonge/fff3cdc43c6e7bf3fe83ba4d7b4b2c51 to your computer and use it in GitHub Desktop.
Mirco-time precise DateTime(Immutable) instances.
<?php
function mirco_time_precise_date_time_immutable()
{
return DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true)));
}
@dnaber-de
Copy link

Via https://twitter.com/tacovdb/status/777762473218801664:

DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', microtime(true)))

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

@tacovandenbroek
Copy link

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 ;)

@svanoers
Copy link

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