DateTimeクラスのsetTimezoneメソッド
メソッド setTimezone は、DateTime オブジェクトのタイムゾーンを変更します。パラメータとして DateTimeZone クラスのオブジェクトを受け取ります。変更された DateTime オブジェクトを返します。
構文
public DateTime::setTimezone(DateTimeZone $timezone): DateTime
例
現在の日付に対して 'America/New_York' タイムゾーンを設定します:
<?php
$date = new DateTime('now', new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
?>
コード実行結果:
'2023-11-15 10:30:00'
例
既存の DateTime オブジェクトのタイムゾーンを変更します:
<?php
$date = new DateTime('2023-01-01 12:00:00', new DateTimeZone('Europe/Moscow'));
$date->setTimezone(new DateTimeZone('Asia/Tokyo'));
echo $date->format('Y-m-d H:i:s');
?>
コード実行結果:
'2023-01-01 18:00:00'
関連項目
-
日付/時刻をフォーマットする関数
date、 -
デフォルトのタイムゾーンを設定する関数
date_default_timezone_set、 -
タイムゾーンを表すクラス
DateTimeZone、