In PHP, date_sunset() is a very practical function to calculate sunset time based on date and geographic location. However, using date_sunset() alone often only gets limited information, such as returning only sunset time. In order to obtain more comprehensive sunshine information (such as sunrise, sunset, sun noon, sunshine duration, etc.), we can encapsulate a custom function sun_info() to integrate the capabilities of date_sun_info() and date_sunset() to output richer sun-related data.
Below, we will show how to implement the sun_info() function and combine date_sunset() to obtain and output detailed sunshine information.
First, we briefly introduce the uses of two key functions:
date_sunset() : Gets the sunset time for the specified time and geographical location.
date_sun_info() : Returns an array containing detailed information such as sunrise, sunset, dusk, dawn, sun noon, etc.
Although date_sun_info() has more comprehensive functionality, we can use the configurability of date_sunset() (such as different zenith angles) to obtain more granular data.
We write a function sun_info() , which takes date, longitude, latitude and time zone as parameters and returns a structured array containing the following information:
Sunrise time
Sunset time
Sunshine length (unit: minutes)
Sun is noon time
Dusk time
dawn time
function sun_info($date, $latitude, $longitude, $timezone = 'UTC') {
$timestamp = strtotime($date);
$sunData = date_sun_info($timestamp, $latitude, $longitude);
$sunset = date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
$sunrise = date_sunrise($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
$daylightMinutes = round(($sunset - $sunrise) / 60);
// Convert to specified time zone formatted output
$dtZone = new DateTimeZone($timezone);
$format = function($ts) use ($dtZone) {
if (!$ts || $ts < 0) return 'N/A';
$dt = new DateTime("@$ts");
$dt->setTimezone($dtZone);
return $dt->format('H:i:s');
};
return [
'sunrise' => $format($sunData['sunrise']),
'sunset' => $format($sunData['sunset']),
'solar_noon' => $format($sunData['solar_noon']),
'civil_dawn' => $format($sunData['civil_twilight_begin']),
'civil_dusk' => $format($sunData['civil_twilight_end']),
'day_length' => $daylightMinutes . ' minute',
];
}
Suppose you want to obtain the Rizhao information from Beijing on April 26, 2025, you can call it like this:
$info = sun_info('2025-04-26', 39.9042, 116.4074, 'Asia/Shanghai');
echo "Beijing Today's Sun Information:<br>";
foreach ($info as $label => $value) {
echo ucfirst(str_replace('_', ' ', $label)) . ': ' . $value . '<br>';
}
An output example might be as follows:
Beijing Today's Sun Information:
Sunrise: 05:21:32
Sunset: 18:57:20
Solar noon: 12:09:26
Civil dawn: 04:53:10
Civil dusk: 19:25:42
Day length: 456 minute
In order to display these sunshine information more intuitively, you can further beautify the output effect by combining JavaScript or chart library. You can also integrate a link on the page, for example:
echo '<a href="https://m66.net/sun?lat=39.9&lon=116.4&date=2025-04-26">See more Rizhao details</a>';