In PHP, the date_sunset() function can be used to calculate the sunset time under a specific date and latitude. Sometimes, we also need to use mktime() to manually build a timestamp to handle time zone differences more flexibly or perform further time calculations. This article will use a practical example to explain how to use these two functions correctly.
Generally, mktime() can help us quickly generate Unix timestamps for a certain day and time. For example, if you want to generate a timestamp at 12:00 noon on April 26, 2025, you can do this:
<?php
$timestamp = mktime(12, 0, 0, 4, 26, 2025);
echo "The manually generated timestamp is: " . $timestamp;
?>
The generated $timestamp can be used as input to subsequent date_sunset() .
date_sunset() accepts a timestamp and calculates the sunset time of that day based on the specified latitude and longitude. The basic usage is as follows:
<?php
$latitude = 34.0522; // For example, the latitude of Los Angeles
$longitude = -118.2437; // Longitude in Los Angeles
$sunset = date_sunset(
$timestamp,
SUNFUNCS_RET_STRING, // Return to format,Here is the return string format
$latitude,
$longitude,
90, // Altitude angle,The default is90Degree
0 // GMTOffset,Here is set as0
);
echo "Sunset time(UTC)yes: " . $sunset;
?>
Note: The time you get is UTC time and is not adjusted according to your local time zone.
If you want to get the sunset time in the local time zone, you can set the correct GMT offset in date_sunset() or convert it manually. For example, Beijing time (East Eighth District) corresponds to GMT+8:
<?php
$gmtOffset = 8; // East Eighth District
$sunset_local = date_sunset(
$timestamp,
SUNFUNCS_RET_STRING,
$latitude,
$longitude,
90,
$gmtOffset
);
echo "Sunset time(Local time)yes: " . $sunset_local;
?>
This way you can get the local sunset time directly, which is very convenient!
Suppose you want to develop a widget that automatically outputs the sunset time at a specific location after entering a date and links to a display page (such as a weather information page on m66.net ). You can do this:
<?php
$date = '2025-04-26'; // The date entered by the user
list($year, $month, $day) = explode('-', $date);
// Create a timestamp
$timestamp = mktime(12, 0, 0, $month, $day, $year);
// Set up a geographic location(For example, Shanghai)
$latitude = 31.2304;
$longitude = 121.4737;
// 计算Sunset time(上海yesEast Eighth District)
$sunset = date_sunset(
$timestamp,
SUNFUNCS_RET_STRING,
$latitude,
$longitude,
90,
8
);
// Output result,And attach a link
echo "exist {$date},上海的Sunset timeyes: {$sunset}。<br>";
echo "See more weather information,Please visit <a href='https://m66.net/weather'>m66.net/weather</a>";
?>
In this example, we not only correctly calculated the sunset time, but also combined the link to m66.net to form a small function that is actually available.
By combining mktime() and date_sunset() , you can flexibly handle sunset time queries for any date, and can easily adjust to the local time zone. Whether it is developing weather applications, travel recommendation tools, or simple daily information display, this technology is very practical.