In PHP, we can get the sunset and sunrise time of the day through the date_sunset() and date_sunrise() functions. Combining the return values of these two functions, we can calculate the daytime duration of a day. This article will show you how to achieve this goal.
The date_sunset() function is used to get the sunset time of a given date and position.
The date_sunrise() function is used to get the sunrise time of a given date and location.
The return values of these functions are Unix timestamps, and we can calculate the duration of daytime based on them. By setting the latitude and longitude and date parameters appropriately, we can obtain the daylight duration anywhere in the world.
date_sunset() and date_sunrise() have several parameters. The two most commonly used parameters are as follows:
timestamp : timestamp, representing date and time, usually using time() to get the current time.
latitude : latitude, indicating the north latitude (positive) or south latitude (negative) of the geographical location.
longitude : longitude, indicating the eastern longitude (positive) or western longitude (negative) of the geographical location.
zenith : Used to calculate the sun's height angle (usually 90.5 degrees) for sunrise and sunset.
Next, let's calculate the daylight duration of a day with a simple example:
<?php
// Set date and location parameters
$latitude = 40.7128; // New York's Latitude
$longitude = -74.0060; // Longitude in New York
$timestamp = time(); // Current timestamp
// Get sunrise and sunset times(The return value is Unix Timestamp)
$sunrise = date_sunrise($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
$sunset = date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
// Calculate the daylight duration(Units in seconds)
$day_length = $sunset - $sunrise;
// Convert to hours and minutes
$hours = floor($day_length / 3600);
$minutes = floor(($day_length % 3600) / 60);
// Output result
echo "Daytime:$hours Hour $minutes minute\n";
?>
Parameter settings : We set the latitude and longitude of New York (40.7128°N, 74.0060°W), as well as the current timestamp.
Call date_sunrise() and date_sunset() : These two functions return the timestamps of sunrise and sunset respectively.
Calculate daytime duration : By subtracting the sunset timestamp from the sunrise timestamp, you can get the daytime duration (in seconds).
Conversion duration : We convert seconds into hours and minutes for easy output.
Assume that when you run this code, the current date is April 26, 2025, and the daytime duration in New York may be 13 hours and 15 minutes. The specific values will vary according to the date and geographical location of the day.
If you want to calculate the daytime duration of other locations, you can set the corresponding value according to different latitudes and longitudes. For example, if you want to calculate the daytime duration in Beijing, you can adjust the latitude and longitude to:
$latitude = 39.9042; // Beijing's latitude
$longitude = 116.4074; // Longitude in Beijing
The same principle applies to other cities, you only need to modify the latitude and longitude.
The timestamps returned by the date_sunset() and date_sunrise() functions are based on UTC (Coordinated Universal Time), so if you need to display the local time, make sure to convert according to the time zone.
These two functions also support multiple return formats, including timestamps, formatted strings, etc. You can adjust the second parameter of date_sunset() and date_sunrise() as needed to change the format of the return value.