In PHP, timezone_open() creates a DateTimeZone object from a time zone name, an offset or an abbreviation.
It is the procedural alias of new DateTimeZone(). The difference is how errors are handled: new DateTimeZone() throws an exception for a bad name, while timezone_open() raises a warning and returns false. You then pass the object to date_create(), timezone_name_get() and other date functions.
Syntax
Call timezone_open() with the values you need. Start with this line.
PHP
$tz = timezone_open('Europe/Paris');
Call
What it does
timezone_open('Europe/Paris')
Creates a zone from a full identifier. This is the best choice because it knows the daylight saving rules.
timezone_open('+05:30')
Creates a zone with a fixed offset from UTC.
timezone_open('EST')
Creates a zone from an abbreviation.
For an unknown name, timezone_open() returns false and triggers a warning. Check the result with === false before you use the object.
timezone_open() builds the zone object for New York.
date_create() reads the date text in that zone.
On July 4 daylight saving time is active, so the abbreviation is EDT.
The offset P shows -04:00, which is four hours behind UTC.
Frequently Asked Questions
It creates a DateTimeZone object from a time zone name, offset or abbreviation. You use that object with other date and time functions.
They do the same job. The difference is the error handling: timezone_open() returns false and raises a warning for a bad name, while new DateTimeZone() throws an exception.
Call timezone_open() and check that the result is not false, or use in_array() with timezone_identifiers_list().
Use a full identifier such as Europe/London. It follows daylight saving rules automatically. Fixed offsets and abbreviations do not.
Pass it to date_create(), date_timezone_set(), timezone_name_get(), timezone_offset_get() or the DateTime constructor.