I need to add a microsecond to a Datetime object in PHP. I am trying to do it adding a time interfal a fraction of a second to the Datetime but it is not working.
$date = new Datetime('2018-06-05 09:06:46.7487');
$date->add(new DateInterval('PT0.00001S'));
echo $date->format('Y-m-d H:i:s.u');
I am not able to accomplish it though i think it should be simple. How can i add fractions of a second to a Datetime?
PHP >= 7.1 - works but there's a bug!
If you have PHP 7.1 or later then this should do it:
Output:
Caution: this fails for
.999999Output:
PHP >= 5.2.0 "hack" but not buggy!
If you have PHP 7.0 or earlier then you can extract the microseconds and perform the math yourself in a "hacky" way:
Output:
The hacky solution also works if the microsecond is at
.999999Output: