Modify microseconds of a PHP DateTime object
I have a PHP DateTime object with microseconds created as follows:
$time = microtime(true);
$microseconds = sprintf('%06d', ($time - floor($time)) * 1000000);
$dt = new DateTime(date('Y-m-d H:i:s.' . $microseconds, $time));
How can I modify the microseconds value of
$dt
, without creating a completely new DateTime instance?Answer:
You can't.
There are three methods that can modify the value of a
DateTime
instance: add
, sub
and modify
. We can rule out add
and sub
immediately because they work in terms of a DateInterval
which does not have sub-second precision.modify
accepts a string in one of the standard recognized formats. Of those formats, only the relative ones are of interest here because the other ones work in an absolute manner; and there is no relative format that allows tweaking the msec part (that unit is not recognized).
http://stackoverflow.com/questions/21756701/modify-microseconds-of-a-php-datetime-object
COMMENTS