DateTimeImmutable::__construct date_create_immutable 新しい DateTimeImmutable オブジェクトを返す &reftitle.description; &style.oop; public DateTimeImmutable::__construct stringdatetime"now" DateTimeZonenulltimezone&null; &style.procedural; DateTimeImmutablefalsedate_create_immutable stringdatetime"now" DateTimeZonenulltimezone&null; 新しい DateTimeImmutable オブジェクトを返します。 &reftitle.parameters; datetime &date.formats.parameter; ここに "now" を指定して $timezone パラメータを使うと、現在時刻を取得できます。 timezone $datetime のタイムゾーンを表す DateTimeZone オブジェクト。 $timezone を省略した場合、 または &null; の場合、 現在のタイムゾーンを使います。 $datetime パラメータが UNIX タイムスタンプ (@946684800 など) であったりタイムゾーン付きで指定した場合 (2010-01-28T15:00:00+02:002010-07-05T06:00:00Z など) は、 $timezone パラメータや現在のタイムゾーンは無視されます。 &reftitle.returnvalues; 新しい DateTimeImmutable のインスタンスを返します。 &reftitle.errors; 無効な日付/時刻の文字列が渡された場合、 DateMalformedStringException がスローされます。 PHP 8.3 より前のバージョンでは、 Exception がスローされていました。 &reftitle.changelog; &Version; &Description; 8.3.0 無効な文字列が渡された場合、 Exception ではなく DateMalformedStringException がスローされるようになりました。 7.1.0 マイクロ秒が '00000' ではなく、実際の値で埋められるようになりました。 &reftitle.examples; <function>DateTimeImmutable::__construct</function> の例 &style.oop; getMessage(); exit(1); } echo $date->format('Y-m-d'); ]]> &example.outputs; &style.procedural; &example.outputs; <function>DateTimeImmutable::__construct</function> の複雑な例 format('Y-m-d H:i:sP') . "\n"; // 指定したタイムゾーンでの日時の指定 $date = new DateTimeImmutable('2000-01-01', new DateTimeZone('Pacific/Nauru')); echo $date->format('Y-m-d H:i:sP') . "\n"; // PHP のデフォルトのタイムゾーンでの現在日時 $date = new DateTimeImmutable(); echo $date->format('Y-m-d H:i:sP') . "\n"; // 指定したタイムゾーンでの現在日時 $date = new DateTimeImmutable('now', new DateTimeZone('Pacific/Nauru')); echo $date->format('Y-m-d H:i:sP') . "\n"; // UNIX タイムスタンプの使用例。結果のタイムゾーンは UTC となることに注意しましょう。 $date = new DateTimeImmutable('@946684800'); echo $date->format('Y-m-d H:i:sP') . "\n"; // 存在しない値は繰り上がります $date = new DateTimeImmutable('2000-02-30'); echo $date->format('Y-m-d H:i:sP') . "\n"; ]]> &example.outputs.similar; 繰り上がった日付は、 DateTimeImmutable::getLastErrors の警告をチェックすることで検知できます。 関連付けられたタイムゾーンを変更する setTimezone($timeZone); echo $time->format('Y/m/d H:i:s e'), "\n"; ]]> &example.outputs.similar; 相対日付/時刻 の文字列を使う format('Y/m/d H:i:s'), "\n"; ]]> &example.outputs.similar;