Getting User Time Zone through IP Address in PHP

When developing web applications, it’s often essential to provide a personalized experience for users based on their geographical location. One crucial aspect of personalization is displaying accurate date and time information in the user’s local time zone.

In this article, we will explore how to retrieve a user’s time zone using their IP address in PHP. This type of functionality can be particularly useful for applications that require time-sensitive features, such as scheduling appointments, displaying event times, or showing localized content.

Understanding IP Addresses and Time Zones

Before starting, let’s briefly understand what an IP address and time zones are. An IP address is a unique identifier assigned to each device connected to a network, such as the Internet. It serves as a virtual address for devices to communicate with each other. On the other hand, time zones represent specific geographical regions that follow the same standard time. Time zones are determined by the Earth’s rotation and are expressed as an offset from Coordinated Universal Time (UTC).

Before starting further, you need to register or log in to Ipgeolocation and generate API keys for authentication.

Getting User Time Zone through IP Address

To determine a user’s time zone using their IP address in PHP, we can leverage a free IP geolocation service and the built-in functionality of the language.

Here’s an example of how you can accomplish this:


function getUserTimezone(){
    $userIP = $_SERVER['REMOTE_ADDR'];

    $apiEndpoint = "" . $userIP;

    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => $apiEndpoint,
    $response = curl_exec($curl);

    $data = json_decode($response, true);

    return $data['timezone'];

$userTimeZone = getUserTimezone();

echo "Your time zone is: " . $userTimeZone;

In the above example, we have created a function that will return the user’s timezone by the user’s IP address. In simple terms, first of all, it will get the user’s IP address then perform an API request to geolocation and return timezone. Finally, we display the user’s time zone on the web page.

The $_SERVER[‘REMOTE_ADDR’] variable is used to get an IP address. Make sure you replace YOUR_API_KEY with your API key from an IP geolocation service provider.

You can place the above code into a separate file and use it whenever required. i.e. your schedule booking page and calendar page are required to display the timezone then you can call the above function.


In the above article, you have learned about what is IP address and Timezone. We have also taken a practical example to get the user’s timezone using an IP address with the help of the Ip GEO Location service. There are several providers available, such as, GeoIP, or IP-API.

By following the simple steps outlined in this article, you can easily implement this functionality in your PHP web applications or extend it as per your requirement.