php8.5
Home/ Manual/ datetime / datetime/ DateTime::setMicrosecond

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

microsecond

The microsecond value to set (0 to 999999).

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

See Also

  • DateTimeInterface::getMicrosecond

Source: reference/datetime/datetime/setmicrosecond.xml · from the official PHP manual (php/doc-en)