Current Location: Home> Latest Articles> Compare date_sunset multiple cities with sunset times and display them in order

Compare date_sunset multiple cities with sunset times and display them in order

M66 2025-05-30

In PHP, the date_sunset() function can be used to obtain the sunset time at a specified location. By using this function, we can easily compare sunset times for multiple cities and present them in order. This article will explain how to obtain sunset times in different cities through PHP and arrange these times in order from morning to night.

1. Introduction to date_sunset() function

date_sunset() is a PHP built-in function for getting sunset time. Its basic syntax is as follows:

 date_sunset(time, format, latitude, longitude, zenith, gmt_offset);
  • time : The Unix timestamp of the current time, usually obtained using the time() function.

  • format : Returns the format of time, which can be SUNFUNCS_RET_TIMESTAMP (returns timestamp), SUNFUNCS_RET_STRING (returns string format), or SUNFUNCS_RET_DOUBLE (returns the number of hours of floating).

  • latitude : The latitude of the target city.

  • longitude : The longitude of the target city.

  • zenith : The standard height of sunset, generally 90.5 degrees (i.e. when the sun contacts the horizon).

  • gmt_offset : The offset of the time zone, usually 0 or other time zones.

2. Get sunset times in multiple cities

We can define their latitude and longitude for each city and use the date_sunset() function to get their sunset time. Suppose we want to get the sunset times of the following cities: Beijing, New York and London.

Here is a sample code for getting sunset time:

 <?php
// Define the latitude and longitude of each city
$cities = [
    "Beijing" => ['latitude' => 39.9042, 'longitude' => 116.4074],
    "New York" => ['latitude' => 40.7128, 'longitude' => -74.0060],
    "London" => ['latitude' => 51.5074, 'longitude' => -0.1278]
];

// Current timestamp
$current_time = time();

// Store the sunset time of each city
$sunset_times = [];

foreach ($cities as $city => $coords) {
    // Get the sunset time for each city,Format as timestamp
    $sunset_timestamp = date_sunset($current_time, SUNFUNCS_RET_TIMESTAMP, $coords['latitude'], $coords['longitude']);
    // Store city and sunset times into an array
    $sunset_times[$city] = $sunset_timestamp;
}

// Sort the sunset times
asort($sunset_times);

// Output sorted results
echo "Sunset time in each city(In order):\n";
foreach ($sunset_times as $city => $sunset) {
    echo $city . " The sunset time is: " . date("Y-m-d H:i:s", $sunset) . "\n";
}
?>

3. Code explanation

  1. Defining the city and latitude and longitude : We specify an array for each city that contains the latitude and longitude information of the city.

  2. Get the current time : Get the current Unix timestamp via time() to calculate the sunset time.

  3. Loop to get sunset time : Use the date_sunset() function to calculate sunset time for each city. Note that SUNFUNCS_RET_TIMESTAMP format is used here to return a timestamp for easier subsequent sorting.

  4. Sort sunset time : Sort arrays in ascending order by sunset time through the asort() function.

  5. Output result : Finally, the timestamp is converted into a readable date-time format through the date() function, and the sunset time of each city is output in order.

4. Output example

Assuming the current time is 2025-04-26 , after running the above code, the output result will be similar to:

 Sunset time in each city(In order):
New York The sunset time is: 2025-04-26 19:52:00
London The sunset time is: 2025-04-26 20:12:00
Beijing The sunset time is: 2025-04-26 18:45:00

5. Things to note

  • The time returned by date_sunset() is UTC-based, ensuring that it is converted to local time when the result is displayed.

  • If you need to support display of multiple time zones, you can set the time zone through the date_default_timezone_set() function.

  • The accuracy of latitude and longitude and time zones affects the accuracy of the results, so make sure to use accurate city coordinates.