DateTime::setMicrosecond
PHP function
Edit on GitHub ✎
(PHP 8 >= 8.4.0)
Sets microsecond part of the time
Description
DateTime::setMicrosecond(int $microsecond): static
Sets microsecond part of the time.
Like DateTimeImmutable::setMicrosecond but works with DateTime.
Parameters
microsecondThe microsecond value to set (
0to999999).
Return Values
Modifiedobject
Errors/Exceptions
If the microsecond is outside the range [0, 999999], a DateRangeError is thrown.
Examples
DateTime::setMicrosecond example
php
<?php
$date = DateTime::createFromTimestamp(123.456789);
echo $date->format('Y-m-d H:i:s.u') . PHP_EOL;
$date->setMicrosecond(987654);
echo $date->format('Y-m-d H:i:s.u') . PHP_EOL;
?>The above example will output:
output
1970-01-01 00:02:03.456789
1970-01-01 00:02:03.987654See Also
DateTimeInterface::getMicrosecond