« DateTimeImmutable::setISODate
DateTimeImmutable::setTime »

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

参考

関連キーワード:
DateTimeImmutable   マイクロ   setMicrosecond   date   時刻   microsecond   format   setISODate   setTime   NoDiscard   message   modify   itself   static   範囲   DateRangeError   createFromTimestamp   参考   DateTimeInterface   getMicrosecond   Unix   タイムスタンプ
関連する関数・ページ:
datetime.setmicrosecond   datetimeimmutable.settimestamp   function.date-create-immutable   datetimeimmutable.createfromtimestamp   class.datetimeimmutable   datetimeimmutable.settime