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:00 や
2010-07-05T06:00:00Z など) は、
$timezone パラメータや現在のタイムゾーンは無視されます。
&reftitle.returnvalues;
新しい DateTimeImmutable のインスタンスを返します。
&return.falseforfailure.style.procedural;
&reftitle.errors;
エラーがあった場合は Exception を発生させます。
&reftitle.changelog;
&Version;
&Description;
7.1.0
マイクロ秒が '00000' ではなく、実際の値で埋められるようになりました。
&reftitle.examples;
DateTimeImmutable::__construct の例
&style.oop;
getMessage();
exit(1);
}
echo $date->format('Y-m-d');
?>
]]>
&style.procedural;
]]>
&examples.outputs;
DateTimeImmutable::__construct の複雑な例
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";
// そのコンピュータのタイムゾーンでの現在日時
$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;
関連付けられたタイムゾーンを変更する
setTimezone($timeZone);
echo $time->format('Y/m/d H:i:s'), "\n";
?>
]]>
&example.outputs.similar;
相対日付/時刻 の文字列を使う
format('Y/m/d H:i:s'), "\n";
?>
]]>
&example.outputs.similar;