Skip to content

timezone_location_get()

What is PHP's timezone_location_get() Function?

The timezone_location_get() function is a built-in PHP function that returns an associative array containing location details for a specified timezone. Introduced in PHP 5.3.0, it provides the following keys: country_code, latitude, longitude, and comments.

Syntax of PHP's timezone_location_get() Function

The basic syntax of the timezone_location_get() function is as follows:

Syntax of PHP's timezone_location_get() Function

php
<?php

timezone_location_get($timezone)

Here, $timezone is the required parameter that specifies the timezone for which the function should return location information.

Parameters of PHP's timezone_location_get() Function

The timezone_location_get() function accepts one parameter, $timezone, which specifies the timezone for which you want to retrieve location information. The timezone can be specified in a variety of formats, including:

  • A string that contains a valid timezone identifier, such as "America/Los_Angeles" or "Europe/London".
  • A DateTimeZone object that represents the timezone.

(Note: In PHP 8.0+, passing an invalid timezone identifier throws a ValueError.)

Examples of PHP's timezone_location_get() Function

Let's take a look at an example that demonstrates how to use the timezone_location_get() function in PHP.

Example: Using a DateTimeZone Object

In this example, we will use a DateTimeZone object to retrieve location information for the "Europe/London" timezone.

How to use timezone_location_get() function in PHP?

php
<?php

$timezone = new DateTimeZone("Europe/London");
$location = timezone_location_get($timezone);
print_r($location);

Output:


console
Array
(
    [country_code] => GB
    [latitude] => 51.5072
    [longitude] => -0.1276
    [comments] => Greenwich Mean Time
)

The function returns an associative array that includes information about the timezone's location.

Conclusion

In summary, PHP's timezone_location_get() function is a useful function for retrieving location information about a specified timezone. By providing latitude, longitude, and other location details, this function can be used to develop more sophisticated applications that require timezone-related information.

Practice

What can you do with the date_timezone_get() function in PHP?

Dual-run preview — compare with live Symfony routes.