PHP Reference ยท Date and Time
The DateTimeZone class
Reference for the PHP DateTimeZone class: named zones, fixed offsets, getOffset, getTransitions, listIdentifiers and converting a DateTime between zones.
A DateTimeZone object describes a time zone: a named region with its own daylight saving rules (Europe/Madrid), a fixed offset (+05:30) or an abbreviation (EST). You pass it to DateTime and DateTimeImmutable to interpret or to display times in that zone.
Always prefer region names. A fixed offset like -05:00 never changes, but New York is -05:00 in winter and -04:00 in summer. Only a named zone knows that, and it also knows about historical changes.
<?php $tz = new DateTimeZone("Europe/Madrid"); echo $tz->getName(); // Europe/Madrid
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
new DateTimeZone(string $timezone) $tz->getName(): string $tz->getOffset(DateTimeInterface $datetime): int $tz->getLocation(): array|false $tz->getTransitions(int $timestampBegin = PHP_INT_MIN, int $timestampEnd = PHP_INT_MAX): array|false DateTimeZone::listIdentifiers(int $timezoneGroup = DateTimeZone::ALL, ?string $countryCode = null): array DateTimeZone::listAbbreviations(): array
| Name | Description |
|---|---|
$timezone |
A region identifier (Europe/Paris), a UTC offset (+02:00), or an abbreviation (CEST). |
$datetime |
The moment for which the offset is calculated, since the offset depends on the date. |
$timezoneGroup |
DateTimeZone::ALL, EUROPE, ASIA, UTC, PER_COUNTRY and similar constants. |
$countryCode |
An ISO 3166-1 code, only with PER_COUNTRY. |
Return value: getOffset() returns seconds east of UTC as an int. getTransitions() returns an array of arrays with ts, time, offset, isdst and abbr; its first entry describes the start of the range.
Exceptions: the constructor throws DateInvalidTimeZoneException (PHP 8.3 and later; Exception in earlier versions) for an unknown name.
Usage notes
- Store UTC, show local. Keep instants in UTC and create a
DateTimeZonefor the viewer only when formatting. - Region names, not abbreviations.
ESTis a fixed offset; it does not becomeEDTin summer.PST,CETand others are ambiguous or incomplete. - Offsets depend on the date. Call
getOffset()with the moment you care about, not with "now". - Validate user input with
in_array($name, DateTimeZone::listIdentifiers(), true). - Setting the zone on an object does not move time.
new DateTimeImmutable("12:00", $tz)means noon in$tz;->setTimezone($tz)keeps the instant and changes the clock. getLocation()can befalsefor zones created from offsets.- Data comes from PHP's bundled tz database, which changes a few times a year when governments change rules.
- Default zone. If you omit the zone, the default from
date_default_timezone_set()applies.