Modify microseconds of a PHP DateTime object

This seems to have been available since 7.1.0-rc4

$dt = new DateTime('2020-01-01 0:00');
$dt->modify('+500 ms'); // Milliseconds.
$dt->modify('+123456 usec'); // Microseconds.
$dt->modify('+123456 microseconds'); // This works too.

Can't find it in the PHP manual though.


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


as of PHP 7.1 DateTime::setTime() supports microseconds.

Tags:

Datetime

Php