What it is and why to use it
A meeting stored in UTC may need to be displayed for learners in London and Tokyo. A time zone supplies the rules for converting an instant into local date and clock fields. DateTimeImmutable::setTimezone() returns a new object with that presentation zone while keeping the same underlying instant. Named zones include historical and daylight-saving rules that a fixed offset cannot describe.
How it works
Create an explicit DateTimeZone and pass it when parsing a date whose text has no zone. Calling setTimezone on an immutable date returns another date, so keep the returned value. format() then uses that object’s zone. getTimestamp() identifies the instant in seconds since the Unix epoch; converting its display zone does not change that timestamp.
Common mistakes and limitations
Do not infer a user’s time zone from a country or a single current offset. A local clock time can be ambiguous or nonexistent near daylight-saving transitions. A future recurring local meeting also needs its zone and scheduling rules; a UTC timestamp alone does not capture those requirements.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$local = $utc->setTimezone(new DateTimeZone('Europe/London'));Examples
Convert one scheduled instant
Display a fixed UTC time in two named time zones.
<?php
declare(strict_types=1);
$utc = new DateTimeImmutable('2026-07-15 12:00:00', new DateTimeZone('UTC'));
$london = $utc->setTimezone(new DateTimeZone('Europe/London'));
$tokyo = $utc->setTimezone(new DateTimeZone('Asia/Tokyo'));
echo $london->format('Y-m-d H:i P'), PHP_EOL;
echo $tokyo->format('Y-m-d H:i P'), PHP_EOL;
echo $utc->getTimestamp() === $tokyo->getTimestamp() ? 'Same instant' : 'Different instant', PHP_EOL;
- The input date has an explicit UTC zone, so its interpretation does not depend on the machine’s default zone.
- London’s summer offset and Tokyo’s offset produce different local clock readings.
- The timestamps remain equal because setTimezone changes the representation, not the instant. Time-zone results depend on the installed time-zone database.
- Expected output: 2026-07-15 13:00 +01:00 2026-07-15 21:00 +09:00 Same instant
See why a zone is more than one offset
Compare London’s offsets on two fixed seasonal dates.
<?php
declare(strict_types=1);
$zone = new DateTimeZone('Europe/London');
$winter = new DateTimeImmutable('2026-01-15 12:00:00', $zone);
$summer = new DateTimeImmutable('2026-07-15 12:00:00', $zone);
echo $winter->format('P'), PHP_EOL;
echo $summer->format('P'), PHP_EOL;
- Both dates use the same named zone, but their dates fall under different offset rules.
- The P formatter prints each offset with a colon.
- Hardcoding +00:00 as a permanent substitute for Europe/London would miss the summer change. Keep system time-zone data current for real scheduling.
- Expected output: +00:00 +01:00
Best practices
- Use explicit named zones for local display and calendar rules.
- Store enough information to distinguish instants from recurring local schedules.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual