Signature
timezone_name_get(DateTimeZone $object): stringPurpose
timezone_name_get() is the procedural form of DateTimeZone::getName(). You give it a DateTimeZone object and it returns that zone's name as a string. The manual lists it as an alias, so the two forms do the same job. Which one you use is a matter of style. Code that already uses procedural date functions such as timezone_open() tends to read more consistently with timezone_name_get(). Object-oriented code usually calls $tz->getName().
Typical uses are logging which zone a value was created with, building a dropdown value from a zone object, and storing the zone next to a timestamp so it can be rebuilt later.
Signature
Parameters and defaults
$object: aDateTimeZoneinstance. It is required and has no default. You can get one fromnew DateTimeZone(), fromtimezone_open(), or fromgetTimezone()on aDateTimeorDateTimeImmutable.
Return value and failure cases
The function returns a string. What the string looks like depends on how the zone was created:
- For an identifier zone such as
Europe/ParisorUTC, you get that identifier. - For a fixed-offset zone such as
+05:30, you get the offset text.
The function does not return false for ordinary use. The one failure case is passing something that is not a DateTimeZone. In PHP 8 that throws a TypeError, for example when you pass the string 'UTC' instead of an object. A string is not converted to a zone automatically. Creating a zone from an invalid name fails earlier, when the DateTimeZone constructor runs, not inside timezone_name_get().
Example 1: names from different kinds of zones
This builds three zones and maps the function over them. The identifier zones keep their identifiers, and the offset zone keeps its offset text.
$zones = [new DateTimeZone('Europe/Paris'), new DateTimeZone('UTC'), new DateTimeZone('+05:30')];
echo json_encode(array_map('timezone_name_get', $zones), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["Europe/Paris","UTC","+05:30"]
Example 2: reading the zone from a date object
A date object remembers the zone it was created with. getTimezone() returns a DateTimeZone, which you can pass straight to the function. The example also confirms that the procedural call and the method return the same string.
date_default_timezone_set('UTC');
$date = new DateTimeImmutable('2024-07-01 12:00:00', new DateTimeZone('America/New_York'));
$tz = $date->getTimezone();
echo json_encode([
'procedural' => timezone_name_get($tz),
'method' => $tz->getName(),
'same' => timezone_name_get($tz) === $tz->getName(),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"procedural":"America/New_York","method":"America/New_York","same":true}
Example 3: passing the wrong type
The parameter must be an object. A plain string is rejected with a TypeError, which you can catch if you accept untrusted input.
try {
echo timezone_name_get('UTC');
} catch (TypeError $e) {
echo get_class($e), "\n";
}TypeError
Edge cases
- The name is not an offset for a point in time. For an identifier zone such as
America/New_York, the name stays the same all year even though the UTC offset changes with daylight saving time. To see the offset at a specific moment, usegetOffset()with a date object. - Offset zones return offset text. A zone created from
+05:30has no region name, so there is no city identifier to retrieve from it. - The default zone is not involved. The function reports the zone stored in the object you pass. It does not look at the value set by
date_default_timezone_set(). To read the default zone name, usedate_default_timezone_get(). - It is a copy of information, not a handle. The returned string is a plain value. Changing it has no effect on the zone object.
Common mistakes
- Passing a string instead of an object.
timezone_name_get('Europe/Paris')throws aTypeError. Create the zone first withnew DateTimeZone('Europe/Paris')ortimezone_open(). - Passing a date object instead of its zone. A
DateTimeis not aDateTimeZone. CallgetTimezone()on it first. - Expecting an abbreviation or offset for identifier zones. The result for
Europe/Parisis notCETor+01:00. Useformat('T')orformat('P')on a date object for those. - Assuming the alias behaves differently. There is no behavioral difference to plan around. If you switch between the function and the method, the result is the same.
- Validating user input too late. Wrap the
DateTimeZoneconstructor intry/catch, because an unknown name fails there. Only after that can you safely pass the object on.
Related functions
DateTimeZone::getName(): the method this function aliases.timezone_open(): procedural way to create aDateTimeZone.date_default_timezone_get(): returns the default zone name used by date functions.date_timezone_get(): returns the zone of a date object.timezone_offset_get(): returns the offset in seconds for a given date.timezone_identifiers_list(): lists the available zone identifiers.
Recap
timezone_name_get() takes a DateTimeZone and returns its name as a string. It is an alias of DateTimeZone::getName(). Identifier zones return their identifier and offset zones return their offset text. A non-object argument throws a TypeError, and the name alone tells you nothing about the offset at a given moment.
Tested examples
$zones = [new DateTimeZone('Europe/Paris'), new DateTimeZone('UTC'), new DateTimeZone('+05:30')];
echo json_encode(array_map('timezone_name_get', $zones), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["Europe/Paris","UTC","+05:30"]
date_default_timezone_set('UTC');
$date = new DateTimeImmutable('2024-07-01 12:00:00', new DateTimeZone('America/New_York'));
$tz = $date->getTimezone();
echo json_encode([
'procedural' => timezone_name_get($tz),
'method' => $tz->getName(),
'same' => timezone_name_get($tz) === $tz->getName(),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
{"procedural":"America/New_York","method":"America/New_York","same":true}
try {
echo timezone_name_get('UTC');
} catch (TypeError $e) {
echo get_class($e), "\n";
}
TypeError