In PHP, the date_sunset() function is used to calculate the sunset time of a given date and place. By default, this function returns the time based on the server's default time zone. If the server time zone setting is different from the target location time zone, it may cause the obtained sunset time to be inaccurate. This article will introduce in detail how to adjust the time zone parameters of the date_sunset() function to ensure that the correct sunset time is obtained.
The basic syntax of the date_sunset() function is as follows:
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
$timestamp : Unix timestamp for datetime
$format : return value format, commonly used string format ( SUNFUNCS_RET_STRING ) or timestamp format ( SUNFUNCS_RET_TIMESTAMP )
$latitude , $longitude : geographic coordinates
$zenith : Sun's height angle, default
$gmt_offset : Time zone offset (number of hours relative to GMT)
date_sunset() The last parameter $gmt_offset is the key to adjusting the time zone. It is the number of hours relative to GMT, such as:
Beijing time (UTC+8) incoming 8
New York Time (UTC-5) Incoming-5
Passing $gmt_offset correctly allows the function to return the exact sunset time of that time zone.
The following example shows how to use date_sunset() to get the sunset time of Beijing time:
<?php
// Set the time zone,Influence date() Output of equal functions,但不Influence date_sunset Calculation of
date_default_timezone_set('Asia/Shanghai');
// Get the timestamp for today
$timestamp = time();
// Set geographic coordinates(Beijing)
$latitude = 39.9042;
$longitude = 116.4074;
// Set the time zone偏移,Beijing时间是 +8
$gmt_offset = 8;
// Get the sunset time,Return the format as a string "HH:MM"
$sunset_time = date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude, 90, $gmt_offset);
echo "Beijing时间的日落时间是:$sunset_time";
?>
This code outputs similar to:
Beijing时间的日落时间是:18:45
$gmt_offset needs to be consistent with the time zone corresponding to $latitude and $longitude .
The PHP server time zone does not affect the time zone calculation of date_sunset , but it will affect the output of date() and other time functions.
If you want to deal with daylight saving time, you need to dynamically adjust $gmt_offset based on dates, or use more complex date and time libraries (such as DateTime combined with DateTimeZone ) to calculate more accurate moments.
date_sunset() does not support automatic identification of daylight saving time. If you need to process time zones more accurately, you can first use DateTime to get the timestamp with time zone, and then pass it in date_sunset() .
Example:
<?php
// Target time zone
$timezone = new DateTimeZone('America/New_York');
// Specify date and time
$date = new DateTime('now', $timezone);
// Get the timestamp
$timestamp = $date->getTimestamp();
// Geographical coordinates(New York)
$latitude = 40.7128;
$longitude = -74.0060;
// Get time zone offset(Hour)
$gmt_offset = $timezone->getOffset($date) / 3600;
// Get the sunset time
$sunset_time = date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude, 90, $gmt_offset);
echo "New York时间的日落时间是:$sunset_time";
?>
This can automatically adjust the offset according to the target time zone to avoid manual calculation errors.
The $gmt_offset parameter of date_sunset() determines the time zone of the returned sunset time.
Manually passing in the correct time zone offset can ensure accurate results.
Combining DateTime and DateTimeZone allows for more flexibility in handling complex time zones and daylight saving time.