In PHP, date_sunrise() and date_sunset() are two very practical functions that can calculate the sunrise and sunset times of the day based on the specified date and geographical coordinates. By combining these two functions, we can calculate the total duration of day very easily.
This is very valuable for application scenarios that require adjustment of business logic according to day length, such as automatic light switching, smart farms, outdoor activity management and other systems.
date_sunrise(int $timestamp, int $format = SUNFUNCS_RET_STRING, float $latitude = ini_get("date.default_latitude"), float $longitude = ini_get("date.default_longitude"), float $zenith = ini_get("date.sunrise_zenith"), float $gmt_offset = 0): string|int|float|false
date_sunset(int $timestamp, int $format = SUNFUNCS_RET_STRING, float $latitude = ini_get("date.default_latitude"), float $longitude = ini_get("date.default_longitude"), float $zenith = ini_get("date.sunset_zenith"), float $gmt_offset = 0): string|int|float|false
Let's use a concrete example to illustrate how to calculate daytime:
Date: Current date
Coordinates: Beijing City (latitude 39.9042, longitude 116.4074)
Time zone: East Eighth District (UTC+8)
<?php
date_default_timezone_set('Asia/Shanghai');
$timestamp = time();
$latitude = 39.9042;
$longitude = 116.4074;
$gmt_offset = 8;
// Get sunrise and sunset times(by UNIX Return in timestamp form)
$sunrise = date_sunrise($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, 90, $gmt_offset);
$sunset = date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude, 90, $gmt_offset);
if ($sunrise !== false && $sunset !== false) {
$dayLength = $sunset - $sunrise;
// Convert to hour and minute format
$hours = floor($dayLength / 3600);
$minutes = floor(($dayLength % 3600) / 60);
echo "Sunrise time: " . date("H:i:s", $sunrise) . PHP_EOL;
echo "Sunset time: " . date("H:i:s", $sunset) . PHP_EOL;
echo "Daytime: {$hours}Hour{$minutes}minute" . PHP_EOL;
} else {
echo "无法计算日出或Sunset time。Please check the coordinates or date settings。";
}
The zenith angle used by date_sunrise() and date_sunset() are 90 degrees by default. This means that the sunrise and sunset time is calculated from the position where the horizon has just risen or fallen. It can be adjusted as needed.
The time zone offset ( $gmt_offset ) must be set correctly to ensure that the output time conforms to the local time.
If the returned false , it is usually due to special circumstances such as abnormal coordinate setting, extreme day or extreme night.
This calculation method can be easily embedded in any time-sensitive system, such as:
Automatic lighting system (such as smart home controller)
Astronomical API interface, such as https://api.gitbox.net/astro/daylength?lat=39.9&lon=116.4
Outdoor weather widget to show real-time daytime changes
Using these solar functions provided by PHP, we can accurately obtain time data related to the solar altitude angle without relying on external services, providing the system with a more intelligent judgment basis.