In many application scenarios, understanding the sunset times in different cities is a common requirement. PHP provides the date_sunset() function, which can calculate the sunset time at a specified location. Combined with the DateTimeZone class, we can easily achieve sunset time comparisons in multiple cities. This article will use sample code to introduce how to use date_sunset() to implement this function with DateTimeZone .
The date_sunset() function is a very convenient date and time function built in PHP to get the sunset time of a specified location. Its basic syntax is as follows:
date_sunset(int $timestamp, int $format = SUNFUNCS_RET_STRING, float $latitude = 0, float $longitude = 0, float $zenith = 90.83, float $gmt_offset = 0)
$timestamp : The specified timestamp.
$format : Returns the format, which can be SUNFUNCS_RET_STRING , SUNFUNCS_RET_TIMESTAMP , SUNFUNCS_RET_DOUBLE , etc.
$latitude and $longitude : Specifies the latitude and longitude of the location.
$zenith : The astronomical position of the sun, defaulting to 90.83 (i.e. the astronomical sunset point).
$gmt_offset : The offset of the time zone.
To get more precise sunset times across the region, we can use PHP's DateTimeZone class, which allows us to specify different time zones. By using the time zone with the date_sunset() function, we are able to calculate the exact sunset time.
The following PHP code example demonstrates how to use date_sunset() with the DateTimeZone class to compare sunset times in different cities. In the example, we selected three cities: New York, London and Beijing for comparison.
<?php
// Define the latitude and longitude of a city and the time zone
$locations = [
'New York' => [
'latitude' => 40.7128,
'longitude' => -74.0060,
'timezone' => 'America/New_York'
],
'London' => [
'latitude' => 51.5074,
'longitude' => -0.1278,
'timezone' => 'Europe/London'
],
'Beijing' => [
'latitude' => 39.9042,
'longitude' => 116.4074,
'timezone' => 'Asia/Shanghai'
]
];
// Current timestamp
$timestamp = time();
// Export sunset times in each city
foreach ($locations as $city => $info) {
// Get the time zone object
$timezone = new DateTimeZone($info['timezone']);
// Calculate the sunset time of the city
$sunset = date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, $info['latitude'], $info['longitude']);
// create DateTime Object and set time zone
$sunset_time = new DateTime("@$sunset");
$sunset_time->setTimezone($timezone);
// Format output sunset time
echo "The sunset time in $city is: " . $sunset_time->format('Y-m-d H:i:s') . " (" . $info['timezone'] . ")\n";
}
?>
We define three cities, their latitude and longitude and time zones. Use the DateTimeZone class to get the time zone object of each city, ensuring that the calculated sunset time is based on local time.
Use the date_sunset() function to calculate the sunset time of each city. This function returns the result in Unix timestamp format.
We set the correct time zone by creating a DateTime object and using the setTimezone() method, and then format the output to the date-time format of Ymd H:i:s .
Assuming the current time is 12:00:00 UTC on April 26, 2025, the program will output something similar to the following:
The sunset time in New York is: 2025-04-26 19:50:00 (America/New_York)
The sunset time in London is: 2025-04-26 20:10:00 (Europe/London)
The sunset time in Beijing is: 2025-04-26 19:30:00 (Asia/Shanghai)
In this way, we can easily compare sunset times in different cities, and the output format is flexible and accurate.