PHP Conference Kansai 2025

DateTimeImmutable::__construct

date_create_immutable

(PHP 5 >= 5.5.0, PHP 7, PHP 8)

DateTimeImmutable::__construct -- date_create_immutable新しい DateTimeImmutable オブジェクトを返す

説明

オブジェクト指向型

public DateTimeImmutable::__construct(string $datetime = "now", ?DateTimeZone $timezone = null)

手続き型

新しい DateTimeImmutable オブジェクトを返します。

パラメータ

datetime

日付/時刻 文字列。有効な書式については 日付と時刻の書式 で説明しています。

ここに "now" を指定して $timezone パラメータを使うと、現在時刻を取得できます。

timezone

$datetime のタイムゾーンを表す DateTimeZone オブジェクト。

$timezone を省略した場合、 または null の場合、 現在のタイムゾーンを使います。

注意:

$datetime パラメータが UNIX タイムスタンプ (@946684800 など) であったりタイムゾーン付きで指定した場合 (2010-01-28T15:00:00+02:002010-07-05T06:00:00Z など) は、 $timezone パラメータや現在のタイムゾーンは無視されます。

戻り値

新しい DateTimeImmutable のインスタンスを返します。

エラー / 例外

無効な日付/時刻の文字列が渡された場合、 DateMalformedStringException がスローされます。 PHP 8.3 より前のバージョンでは、 Exception がスローされていました。

変更履歴

バージョン 説明
8.3.0 無効な文字列が渡された場合、 Exception ではなく DateMalformedStringException がスローされるようになりました。
7.1.0 マイクロ秒が '00000' ではなく、実際の値で埋められるようになりました。

例1 DateTimeImmutable::__construct() の例

オブジェクト指向型

<?php
try {
$date = new DateTimeImmutable('2000-01-01');
} catch (
Exception $e) {
echo
$e->getMessage();
exit(
1);
}

echo
$date->format('Y-m-d');
?>

手続き型

<?php
$date
= date_create('2000-01-01');
if (!
$date) {
$e = date_get_last_errors();
foreach (
$e['errors'] as $error) {
echo
"$error\n";
}
exit(
1);
}

echo
date_format($date, 'Y-m-d');
?>

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

2000-01-01

例2 DateTimeImmutable::__construct() の複雑な例

<?php
// そのコンピュータのタイムゾーンでの日時の指定
$date = new DateTimeImmutable('2000-01-01');
echo
$date->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";
?>

上の例の出力は、 たとえば以下のようになります。

2000-01-01 00:00:00-05:00
2000-01-01 00:00:00+12:00
2010-04-24 10:24:16-04:00
2010-04-25 02:24:16+12:00
2000-01-01 00:00:00+00:00
2000-03-01 00:00:00-05:00

例3 関連付けられたタイムゾーンを変更する

<?php
$timeZone
= new \DateTimeZone('Asia/Tokyo');
$time = new \DateTimeImmutable();
$time = $time->setTimezone($timeZone);
echo
$time->format('Y/m/d H:i:s'), "\n";
?>

上の例の出力は、 たとえば以下のようになります。

2022/08/12 23:49:23

例4 相対日付/時刻 の文字列を使う

<?php
$time
= new \DateTimeImmutable("-1 year");
echo
$time->format('Y/m/d H:i:s'), "\n";
?>

上の例の出力は、 たとえば以下のようになります。

2021/08/12 15:43:51
add a note

User Contributed Notes 2 notes

up
3
Dmitrii
2 years ago
"If $timezone is omitted or null, the current timezone will be used." - note, that timezone IS NOT equal offset, if its important for your application.

If default timezone = Europe/Moscow, then:
echo (new \DateTimeImmutable('2014-10'))->format(DATE_ATOM); // gives "2014-10-01T00:00:00+04:00"
echo (new \DateTimeImmutable('2014-11'))->format(DATE_ATOM); // gives "2014-11-01T00:00:00+03:00"
because of law changes (abolition of "summer time").
up
0
theking2 at king dot ma
9 days ago
Working on a (REST) interface between JavaScript and a database needs to take care of the problem of the time zone info. In JavaScript JSON.stringify() will convert all dates to UTC. Makes sense. If we receive and decode this on in the PHP realm we should explicitly say so and altought null indicates the configured locale timezone although I do believe that more often than not no timezone is configured in PHP. So better be save than sorry and prevent you to miss your flight:

<?php
$jsonString
= '{ "date": "2025-05-04T11:58:37.848Z" }';
$dateString = json_decode($jsonString, true)['date']; // will contain the date in UTC (Zulu) tz

$d = new \DateTimeImmutable($dateString, new \DateTimeZone('UTC')); // interpreted as such

$databaseZone = new \DateTimeZone("Europe/Zurich");
$d = $d->setTimeZone( $databaseZone ); // but our server is somewhere else

var_dump($d);

// Now we can store the date in our local database, which is blissfully unaware of timezones

?>
To Top