php8.5
Home/ Manual/ datetime / datetime/ DateTime::setTimezone

DateTime::setTimezone

PHP function Edit on GitHub ✎

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

Sets the time zone for the DateTime object

Description

Oop

DateTime::setTimezone(DateTimeZone $timezone): DateTime

Procedural

date_timezone_set(DateTime $object, DateTimeZone $timezone): DateTime

Sets a new timezone for a DateTime object.

Like DateTimeImmutable::setTimezone but works with DateTime.

The procedural version takes the DateTime object as its first argument.

Parameters

timezone

A DateTimeZone object representing the desired time zone.

Return Values

Returns the DateTime object for method chaining. The underlying point-in-time is not changed when calling this method.

Examples

DateTime::setTimeZone() example

Oop

php
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";

The above example will output:

output
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

Procedural

php
<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";

date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";

The above example will output:

output
2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

See Also

Source: reference/datetime/datetime/settimezone.xml · from the official PHP manual (php/doc-en)