DateTimeImmutable::setMicrosecond

(PHP 8 >= 8.4.0)

DateTimeImmutable::setMicrosecond時刻のマイクロ秒部分を設定する

説明

#[\NoDiscard(message: "as DateTimeImmutable::setMicrosecond() does not modify the object itself")]
public function DateTimeImmutable::setMicrosecond(int $microsecond): static

元のオブジェクトを基にマイクロ秒部分を変更した、 新しい DateTimeImmutable オブジェクトを返します。

パラメータ

microsecond
設定するマイクロ秒の値 (0 から 999999)。

戻り値

変更されたデータを持つ、新しい DateTimeImmutable オブジェクトを返します。

エラー / 例外

microsecond が [0, 999999] の範囲外の場合、DateRangeError がスローされます。

例1 DateTimeImmutable::setMicrosecond() の例

<?php
$date
= DateTimeImmutable::createFromTimestamp(123.456789);
echo
$date->format('Y-m-d H:i:s.u') . PHP_EOL;
$date = $date->setMicrosecond(987654);
echo
$date->format('Y-m-d H:i:s.u') . PHP_EOL;
?>

上の例の出力は以下となります。

1970-01-01 00:02:03.456789
1970-01-01 00:02:03.987654

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top