DateTimeクラスのsetTimezoneメソッド

メソッド setTimezone は、DateTime オブジェクトのタイムゾーンを変更します。パラメータとして DateTimeZone クラスのオブジェクトを受け取ります。変更された DateTime オブジェクトを返します。

構文

phppublic DateTime::setTimezone(DateTimeZone $timezone): DateTime

例

現在の日付に対して 'America/New_York' タイムゾーンを設定します:

php<?php
  $date = new DateTime('now', new DateTimeZone('UTC'));
  $date->setTimezone(new DateTimeZone('America/New_York'));
  echo $date->format('Y-m-d H:i:s');
?>

コード実行結果:

php'2023-11-15 10:30:00'

例

既存の DateTime オブジェクトのタイムゾーンを変更します:

php<?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');
?>

コード実行結果:

php'2023-01-01 18:00:00'

関連項目