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