Current Location: Home> Latest Articles> How to use date_sunset() on different latitudes and longitudes

How to use date_sunset() on different latitudes and longitudes

M66 2025-05-31

In PHP, we can use the date_sunset() function to calculate the sunset time. This function is very useful, especially in applications involving geographic locations, such as weather applications, travel planning or astronomical observation. This article will introduce how to calculate sunset time based on different latitudes and longitudes, and provide relevant application scenarios based on this information.

What is the date_sunset() function?

date_sunset() is a built-in PHP function that calculates sunset time at specified date and latitude and longitude. The signature of this function is as follows:

 date_sunset ( int $timestamp , int $format , float $latitude , float $longitude , float $zenith = 90.5 , float $gmt_offset = 0 )
  • $timestamp : timestamp (the current timestamp can be obtained using the time() function).

  • $format : The output format can be DATE_RSS (default) or DATE_ATOM , etc.

  • $latitude : Latitude (floating value, negative number indicates the southern hemisphere).

  • $longitude : longitude (floating value, negative number indicates the western hemisphere).

  • $zenith : The astronomical height of the sun, the default is 90.5.

  • $gmt_offset : Time zone offset, default is 0.

Calculate sunset time using date_sunset() function

Suppose you have a set of latitude and longitude and want to calculate the sunset time of a place, the following code example shows how to do this:

 <?php
// Set latitude and longitude(For example, the latitude and longitude of Beijing)
$latitude = 39.9042; // latitude
$longitude = 116.4074; // longitude

// Get the current timestamp
$timestamp = time();

// Calculate the sunset time,Format usage DATE_RSS
$sunset = date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude);

// Output calculation results
echo "Sunset time: " . $sunset;
?>

Explain the code

  1. Set latitude and longitude : In the above code, we used latitude and longitude of Beijing (39.9042°N, 116.4074°E). You can replace it with the latitude and longitude of any city or location as needed.

  2. Timestamp : Get the current timestamp through the time() function.

  3. Calculate sunset time : calculate sunset time through the date_sunset() function, and the return value format is DATE_RSS format, which is a standard time format.

  4. Output result : The calculated sunset time by echo output.

How to calculate the sunset time based on different latitudes and longitudes?

If you want to calculate the sunset times of multiple locations based on different latitudes and longitudes, it can be achieved through loops. For example, we have a group of cities and their latitude and longitude:

 <?php
// 定义一个包含城市经latitude的数组
$cities = [
    'Beijing' => [39.9042, 116.4074],
    'New York' => [40.7128, -74.0060],
    'London' => [51.5074, -0.1278],
    'Sydney' => [-33.8688, 151.2093],
];

// Get the current timestamp
$timestamp = time();

// Circularly traverse each city,Calculate the sunset time
foreach ($cities as $city => $coords) {
    $latitude = $coords[0];
    $longitude = $coords[1];
    
    // Calculate the sunset time
    $sunset = date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude);
    
    // 输出每个城市的Sunset time
    echo "{$city} 的Sunset time: " . $sunset . "<br>";
}
?>

result

This code will calculate the sunset time of each city in turn and output it to a web page. The output may be similar to:

 Beijing 的Sunset time: Sun, 26 Apr 2025 18:38:00 +0000
New York 的Sunset time: Sun, 26 Apr 2025 19:45:00 +0000
London 的Sunset time: Sun, 26 Apr 2025 19:30:00 +0000
Sydney 的Sunset time: Sun, 26 Apr 2025 07:20:00 +0000

Things to note

  • Time zone problem : date_sunset() returns UTC time, you may need to adjust it according to the time zone of the target location.

  • Daylight Saving Time : During daylight Saving Time, sunset time is affected, so the deviation of daylight Saving Time needs to be considered when calculating.

  • Date format : The format returned by date_sunset() can be customized as needed, such as DATE_ATOM or DATE_RSS , and you can choose the format that suits you best.

Hopefully this article helps you better understand how to use the date_sunset() function in PHP to calculate sunset time based on latitude and longitude. If you have any other questions, please visit our website!