timezone_open()
The timezone_open function is a PHP built-in function that is used to create a new DateTimeZone object. This function takes a single parameter, which is the
The PHP timezone_open() Function
timezone_open() creates a new DateTimeZone object from a timezone identifier. It is the procedural-style alias of new DateTimeZone() — both do exactly the same thing, so you can use whichever style fits your codebase.
A DateTimeZone object on its own does not "do" anything. Its job is to be handed to a DateTime object so that timestamps are interpreted and displayed in the correct zone. This is what makes timezone_open() useful: it lets one script render the same instant in time correctly for users in New York, Paris, or Tokyo.
Syntax
timezone_open(string $timezone): DateTimeZone|falseParameter
$timezone— one timezone identifier. Use a full IANA name such as"America/New_York","Europe/Paris", or"UTC". Abbreviations like"EST"are accepted but ambiguous and best avoided. The full list comes fromtimezone_identifiers_list().
Return value
- A
DateTimeZoneobject on success. falseif the identifier is not recognized (aWarningis also raised). Because of this, always validate user-supplied identifiers before passing them in.
Practical Examples
Example 1: Setting the default timezone
timezone_open() does not change the script's default timezone — that is the job of date_default_timezone_set(). Set it once near the top of your script so any DateTime created without an explicit zone uses the right one:
<?php
date_default_timezone_set('America/New_York');
echo date_default_timezone_get(); // America/New_YorkExample 2: Creating a DateTime with a specific timezone
Pass the object returned by timezone_open() as the second argument to the DateTime constructor. 'now' is then interpreted in that zone:
The trailing P in the format prints the UTC offset (for example +02:00), so you can confirm the zone was applied.
Example 3: Converting between timezones
A DateTime always stores an absolute instant. Calling setTimezone() does not move the instant — it only changes how that instant is displayed. Here noon in New York is shown as the equivalent wall-clock time in Paris:
The clock advances by six hours because Paris is six hours ahead of New York in January.
Example 4: Guarding against an invalid identifier
Because timezone_open() returns false on failure, check the result before you use it — especially when the identifier comes from user input:
<?php
$tz = @timezone_open('Mars/Olympus_Mons');
if ($tz === false) {
echo 'Unknown timezone, falling back to UTC.';
$tz = timezone_open('UTC');
}
echo "\n", $tz->getName(); // UTCWhen to use timezone_open()
- Displaying one instant in several zones — store everything in UTC, then convert on output with
setTimezone(). - Reading the offset or zone name of a
DateTimeviatimezone_name_get()ortimezone_offset_get(). - Procedural codebases that prefer
function()calls overnewfor consistency.
If you write object-oriented PHP, new DateTimeZone('Europe/Paris') reads more naturally and behaves identically.
Conclusion
timezone_open() builds a DateTimeZone object from an identifier so that DateTime values are interpreted and rendered in the correct zone. The key ideas: it is interchangeable with new DateTimeZone(), it returns false on a bad identifier (so validate input), and setTimezone() changes only the displayed time, never the underlying instant. For the broader picture, see PHP Timezones and PHP Date and Time.