timezone_location_get()
PHP timezone_location_get() returns an array with country code, latitude, longitude, and comments for a DateTimeZone. Syntax, examples, and gotchas.
PHP timezone_location_get() Function
The timezone_location_get() function returns the geographical location associated with a timezone — its country, latitude, and longitude. This is handy when you have a timezone (for example, the one attached to a user's account) and you need to know where on Earth it points, rather than just how its clock is offset.
This page covers what the function returns, its syntax and parameter, runnable examples, common gotchas, and how it relates to PHP's other timezone helpers.
Syntax
<?php
timezone_location_get(DateTimeZone $object): array|falsetimezone_location_get() is the procedural alias of the DateTimeZone::getLocation() method, so $tz->getLocation() and timezone_location_get($tz) are interchangeable.
Parameter
| Parameter | Type | Description |
|---|---|---|
$object | DateTimeZone | Required. A DateTimeZone object whose location you want to read. |
Note that — unlike many other timezone functions — this one takes a DateTimeZone object, not a timezone string. To build the object from a string, use new DateTimeZone("Europe/London") or timezone_open().
Return Value
On success the function returns an associative array with these keys:
| Key | Description |
|---|---|
country_code | Two-letter ISO 3166 country code (e.g. GB), or ?? when unknown. |
latitude | Latitude in decimal degrees. |
longitude | Longitude in decimal degrees. |
comments | A short note about the location; often an empty string. |
It returns false if location information is not available for the given timezone.
Examples
Reading a timezone's location
Here we build a DateTimeZone for "Europe/London" and print its location data:
Output:
Array
(
[country_code] => GB
[latitude] => 51.50833
[longitude] => -0.12528
[comments] =>
)The function returns an associative array describing where the timezone is anchored. Note that comments is empty for many zones — don't rely on it being populated.
Using the object-oriented equivalent
timezone_location_get($tz) is just an alias for DateTimeZone::getLocation(). The following prints the same array:
<?php
$timezone = new DateTimeZone("America/Los_Angeles");
print_r($timezone->getLocation());Output:
Array
(
[country_code] => US
[latitude] => 34.05222
[longitude] => -118.24278
[comments] => Pacific
)Computing the distance between two timezones
Because the function gives you real coordinates, you can do geographic math with them — for example, the great-circle distance (in kilometres) between two zones:
<?php
$a = timezone_location_get(new DateTimeZone("Europe/London"));
$b = timezone_location_get(new DateTimeZone("America/New_York"));
$earthRadius = 6371; // km
$dLat = deg2rad($b["latitude"] - $a["latitude"]);
$dLon = deg2rad($b["longitude"] - $a["longitude"]);
$h = sin($dLat / 2) ** 2
+ cos(deg2rad($a["latitude"])) * cos(deg2rad($b["latitude"]))
* sin($dLon / 2) ** 2;
$distance = 2 * $earthRadius * asin(sqrt($h));
echo round($distance) . " km";Output:
5570 kmCommon Gotchas
- It needs an object, not a string. Passing
"Europe/London"directly raises aTypeError. Wrap the string innew DateTimeZone(...)first. commentsis frequently empty. Treat it as optional metadata, not a label you can show to users.- Offset-only zones have no location. A
DateTimeZonecreated from an offset such as"+02:00"is not tied to a place, so the call returnsfalse. - Invalid identifiers fail early. In PHP 8.0+, constructing
new DateTimeZone("Not/AZone")throws aDateInvalidTimeZoneException(aValueErrorbefore that), so the error surfaces at construction time, not intimezone_location_get().
Related Functions
timezone_open()— create aDateTimeZonefrom a string.timezone_name_get()— get a timezone's name.timezone_offset_get()— get the UTC offset of a timezone.timezone_identifiers_list()— list all supported timezone identifiers.- PHP Timezones — overview of working with timezones in PHP.
Conclusion
timezone_location_get() turns a DateTimeZone object into concrete geographic data — country code, latitude, and longitude — which you can use for mapping, distance calculations, or simply showing users where a timezone lives. Remember to pass a DateTimeZone object (not a string) and to handle the false return for offset-only zones.