When developing calendar applications or weather forecasting functions, we often need to display the sunset time every day. Fortunately, PHP has built-in very convenient functions to help us get sunset time without relying on complex APIs. This article will teach you how to use PHP to get the next 7 days of sunset times and is suitable for displaying directly on the calendar.
PHP provides the date_sunset() function, which can calculate the sunset time of the day based on the specified date, geographical location (latitude and longitude). The basic call method is as follows:
$sunset = date_sunset(time(), SUNFUNCS_RET_STRING, 40.7128, -74.0060);
echo "Today's sunset time is:" . $sunset;
Parameter description here:
time() : Current timestamp.
SUNFUNCS_RET_STRING : Returns the format of time (string).
40.7128 : Latitude (such as the latitude of New York City).
-74.0060 : Longitude.
But what we want is the sunset time for the next 7 days , so we need to expand a little bit.
The complete sample code below shows how to loop out a list of sunset times for the next week:
<?php
date_default_timezone_set('America/New_York'); // Set the correct time zone as needed
// Set the geographical location(For example, New York)
$latitude = 40.7128;
$longitude = -74.0060;
$sunset_times = [];
for ($i = 0; $i < 7; $i++) {
$timestamp = strtotime("+$i day");
$sunset = date_sunset(
$timestamp,
SUNFUNCS_RET_STRING,
$latitude,
$longitude,
90, // The sun is below the horizon(Standard value 90)
date("Z", $timestamp)
);
$sunset_times[] = [
'date' => date('Y-m-d', $timestamp),
'sunset' => $sunset
];
}
// Output result
foreach ($sunset_times as $day) {
echo "date:" . $day['date'] . ",Sunset time:" . $day['sunset'] . "<br>";
}
?>
date_default_timezone_set() must be set correctly, otherwise the returned sunset time may be deviated.
date("Z", $timestamp) is used to automatically match daylight saving time changes that may occur on different dates.
90 is the standard solar altitude angle and is not recommended to change unless there are special needs.
Suppose you want to obtain the sunset time data for the next 7 days from the server through an interface for the front-end to use, you can write a simple API like this:
<?php
header('Content-Type: application/json');
date_default_timezone_set('America/New_York');
$latitude = 40.7128;
$longitude = -74.0060;
$sunset_times = [];
for ($i = 0; $i < 7; $i++) {
$timestamp = strtotime("+$i day");
$sunset = date_sunset(
$timestamp,
SUNFUNCS_RET_STRING,
$latitude,
$longitude,
90,
date("Z", $timestamp)
);
$sunset_times[] = [
'date' => date('Y-m-d', $timestamp),
'sunset' => $sunset
];
}
echo json_encode($sunset_times);
?>
The front-end can then access the interface, for example:
fetch('https://m66.net/api/sunset_times.php')
.then(response => response.json())
.then(data => {
console.log(data);
});
In this way, you can dynamically display the sunset time of the next week on your web calendar!
Through date_sunset() , we can calculate sunset time very conveniently in PHP, without relying on third-party APIs, which are fast, stable and reliable. If you need more accurate astronomical data (such as considering atmospheric refraction, terrain occlusion, etc.), you can also consider integrating professional astronomical computing libraries in the future. But for general application scenarios, this method is more than enough.