W3docs

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|false

timezone_location_get() is the procedural alias of the DateTimeZone::getLocation() method, so $tz->getLocation() and timezone_location_get($tz) are interchangeable.

Parameter

ParameterTypeDescription
$objectDateTimeZoneRequired. 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:

KeyDescription
country_codeTwo-letter ISO 3166 country code (e.g. GB), or ?? when unknown.
latitudeLatitude in decimal degrees.
longitudeLongitude in decimal degrees.
commentsA 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:

php— editable, runs on the server

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 km

Common Gotchas

  • It needs an object, not a string. Passing "Europe/London" directly raises a TypeError. Wrap the string in new DateTimeZone(...) first.
  • comments is frequently empty. Treat it as optional metadata, not a label you can show to users.
  • Offset-only zones have no location. A DateTimeZone created from an offset such as "+02:00" is not tied to a place, so the call returns false.
  • Invalid identifiers fail early. In PHP 8.0+, constructing new DateTimeZone("Not/AZone") throws a DateInvalidTimeZoneException (a ValueError before that), so the error surfaces at construction time, not in timezone_location_get().

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.

Practice

Practice
What does timezone_location_get() return for a valid DateTimeZone?
What does timezone_location_get() return for a valid DateTimeZone?
Was this page helpful?