In PHP, the date_sunset() function is used to retrieve the sunset time for a specific date and location. This is useful for developing weather forecasts, daily schedules, or geographically-related applications. Below, we will explain in detail how to use this function and provide a practical example.
date_sunset() function's basic prototype is as follows:
date_sunset ( int $timestamp , int $returnFormat = SUNFUNCS_RET_STRING , float $latitude , float $longitude , float $zenith = 90.5 , float $gmt_offset = 0 )
$timestamp: This parameter represents the specific timestamp. If you pass a future timestamp, the function will return the sunset time for that future date.
$returnFormat: This parameter determines the format of the return value. The default value is SUNFUNCS_RET_STRING, which returns the time in string format (e.g., "18:30:00"). You can also choose to return a Unix timestamp format or an array with the time information.
$latitude and $longitude: These two parameters specify the latitude and longitude of the observation location. With these parameters, PHP can calculate the sunset time for a specific location.
$zenith: This parameter represents the astronomical position of the sun. The default value is 90.5, representing the sunset angle as defined by astronomy. You can adjust this value as needed.
$gmt_offset: This parameter represents the time difference from GMT, with a default value of 0.
The following example code demonstrates how to use the date_sunset() function to get the sunset time for a specific date (e.g., "2025-04-25") at a specific location (e.g., Beijing).
<?php
// Set timezone
date_default_timezone_set('Asia/Shanghai');
<p>// Input date and coordinates (Beijing coordinates)<br>
$date = '2025-04-25';<br>
$latitude = 39.9042; // Beijing latitude<br>
$longitude = 116.4074; // Beijing longitude</p>
<p>// Convert the date to a timestamp<br>
$timestamp = strtotime($date);</p>
<p>// Get sunset time using date_sunset()<br>
$sunset = date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude);</p>
<p>// Output sunset time<br>
echo "The sunset time for the date $date is: $sunset";<br>
?><br>
Set Timezone: We set the timezone to "Asia/Shanghai" using the date_default_timezone_set() function to ensure the time calculation is correct.
Date and Location: We specify a date (April 25, 2025) and the coordinates for Beijing.
Convert to Timestamp: We use the strtotime() function to convert the date string into a timestamp.
Call date_sunset(): This function calculates the sunset time based on the provided timestamp, coordinates, and other parameters. We choose to return the time in string format.
Output Sunset Time: Finally, the calculated sunset time is outputted.
If you run the above code, the output will be similar to:
The sunset time for the date 2025-04-25 is: 18:31:00
date_sunset() also supports different return formats, depending on the $returnFormat parameter you pass. In addition to SUNFUNCS_RET_STRING (string format), you can use the following constants:
SUNFUNCS_RET_TIMESTAMP: Returns the time in Unix timestamp format.
SUNFUNCS_RET_ARRAY: Returns an array containing the sunset time and related information.
For example, to use SUNFUNCS_RET_TIMESTAMP to return the Unix timestamp format:
$sunset_timestamp = date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
echo "Sunset time in Unix timestamp format: $sunset_timestamp";
date_sunset() function may not be applicable for all dates. If the given date cannot calculate the sunset time (e.g., during summer or winter in polar regions), it may return false.
The choice of latitude and longitude is crucial, as the sunset time depends on your geographical location.