Current Location: Home> Latest Articles> How to set different time zones to get the correct sunset time

How to set different time zones to get the correct sunset time

M66 2025-05-17

Time zone setting is a crucial factor when developing PHP applications that use sunset time-related features. Because the sunset times are different in different time zones, when obtaining accurate sunset times, it is necessary to ensure that the time zone is set correctly. This article will show how to set different time zones in PHP and get the exact sunset time. We will achieve this through PHP code, combined with external APIs.

1. Set the time zone

PHP's date_default_timezone_set() function allows us to set the default time zone. By default, PHP uses the system's time zone settings, but we can override it with this function. For example, the following code sets the time zone to New York time:

 <?php
date_default_timezone_set('America/New_York');
echo 'The current time is:' . date('Y-m-d H:i:s');
?>

Common time zone identifiers are:

  • Asia/Shanghai (Shanghai time)

  • Europe/London (London time)

  • America/New_York (New York time)

You can find the complete time zone list in the PHP time zone list .

2. Get the exact sunset time

In order to get the exact sunset time, we usually need to call a third-party API service. A commonly used API is the Sunset and Sunrise Times API . Through this API, we can pass latitude and longitude and time zone information to obtain the sunset time of a certain place.

Here is an example of using this API to get the sunset time in a certain place:

 <?php
// Set the time zone
date_default_timezone_set('Asia/Shanghai');

// Set latitude and longitude
$latitude = 31.2304;  // Shanghai&#39;s Latitude
$longitude = 121.4737; // Longitude of Shanghai

// useAPIGet the sunset time
$api_url = "https://api.sunset-sunrise.org/json?lat={$latitude}&lng={$longitude}&formatted=0";
$response = file_get_contents($api_url);
$data = json_decode($response, true);

// Analyze the sunset time
$sunset_time = $data['results']['sunset'];
echo 'The sunset time in Shanghai is:' . $sunset_time;
?>

In this code, we use the file_get_contents() function to send the API request and parse the returned JSON data through json_decode() . $data['results']['sunset'] contains the UTC time of sunset. We can adjust this time according to the set time zone to ensure that it meets the local time.

3. Convert UTC time to local time

The obtained sunset time is usually UTC time, which we need to convert to the local time of the target time zone. This can be achieved using the DateTime class and the DateTimeZone class.

Here is a sample code for how to convert UTC time to a local time zone:

 <?php
// Get the sunset time(UTC)
$utc_sunset_time = $data['results']['sunset']; // Assume fromAPIObtained inUTCtime

// createDateTimeObject and set asUTCTime zone
$datetime = new DateTime($utc_sunset_time, new DateTimeZone('UTC'));

// 转换为上海Time zone
$datetime->setTimezone(new DateTimeZone('Asia/Shanghai'));

// 输出转换后的本地time
echo 'The sunset time in Shanghai is:' . $datetime->format('Y-m-d H:i:s');
?>

In this example, we first create a DateTime object and set it to the UTC time zone through the DateTimeZone class. Then, use the setTimezone() method to convert the time to the Shanghai time zone ( Asia/Shanghai ). Finally, use the format() method to output the local time.

4. Complete code example

Combining all the previous sections, here is a complete PHP code example showing how to set the time zone and get the exact sunset time:

 <?php
// Set the time zone为上海
date_default_timezone_set('Asia/Shanghai');

// Set latitude and longitude
$latitude = 31.2304;
$longitude = 121.4737;

// useAPIGet the sunset time
$api_url = "https://api.sunset-sunrise.org/json?lat={$latitude}&lng={$longitude}&formatted=0";
$response = file_get_contents($api_url);
$data = json_decode($response, true);

// GetUTCtime
$utc_sunset_time = $data['results']['sunset'];

// createDateTimeObject and set asUTCTime zone
$datetime = new DateTime($utc_sunset_time, new DateTimeZone('UTC'));

// 转换为上海Time zone
$datetime->setTimezone(new DateTimeZone('Asia/Shanghai'));

// 输出日落time
echo 'The sunset time in Shanghai is:' . $datetime->format('Y-m-d H:i:s');
?>

Summarize

By setting the time zone and using the API to get the sunset time, we can get the sunset time in different regions accurately. It is important to make sure the time zone is set correctly so that the local accurate sunset time is available. At the same time, leveraging the DateTime class and the DateTimeZone class can help us convert UTC time to the local time of the target time zone.