In PHP, the date_sunset() function can easily calculate the sunset time of a specific date and place. This function is particularly important in websites that need to display information related to Rizhao, such as weather websites, travel service websites, etc.
However, each call of date_sunset() requires complex astronomical calculations. If your website has a large number of visits and frequently calls this function, it may have a certain impact on the server performance. In order to optimize, we can combine simple caching technology to reduce duplicate calculations and greatly improve the website response speed.
This article will explain how to use date_sunset() and combine caching technology to create high-performance sunset time query.
Let’s first look at a basic example:
<?php
$date = strtotime('today');
$latitude = 40.7128; // New York City Latitude
$longitude = -74.0060; // New York City Longitude
$sunset = date_sunset($date, SUNFUNCS_RET_STRING, $latitude, $longitude, 90, date('Z') / 3600);
echo "Today's sunset time is:$sunset";
?>
The above code will return today's sunset time in New York City, such as 19:45 .
Suppose your website page will recalculate the sunset time every time it is refreshed, even in the same city or on the same day, which will cause unnecessary waste of server resources.
In fact, the sunset time on the same place and the same day is fixed. Therefore, we can store the results in the cache after the first calculation. The next time you request, the cache is read directly and no longer repeated calculations.
The following example shows how to use file caching:
<?php
function getSunsetTime($latitude, $longitude) {
$cacheDir = __DIR__ . '/cache/';
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0777, true);
}
$cacheKey = md5($latitude . '_' . $longitude . '_' . date('Y-m-d')) . '.txt';
$cacheFile = $cacheDir . $cacheKey;
if (file_exists($cacheFile)) {
$sunset = file_get_contents($cacheFile);
} else {
$date = strtotime('today');
$sunset = date_sunset($date, SUNFUNCS_RET_STRING, $latitude, $longitude, 90, date('Z') / 3600);
file_put_contents($cacheFile, $sunset);
}
return $sunset;
}
// Example of usage
$latitude = 34.0522; // Los Angeles Latitude
$longitude = -118.2437; // Los Angeles Longitude
$sunset = getSunsetTime($latitude, $longitude);
echo "Today's Los Angeles Sunset Time:$sunset";
?>
The logic of this program is:
Check whether the cache directory exists, and create it if it does not exist.
Generate cached key names based on location and date.
If the cache file exists, read it directly.
If it does not exist, call date_sunset() to calculate and store it in the cache.
Through this method, the same location is calculated only once in the same day, greatly reducing the burden on the server.
If you want users to dynamically query the sunset times in different cities through URLs, you can do this:
<?php
$latitude = isset($_GET['lat']) ? floatval($_GET['lat']) : 0;
$longitude = isset($_GET['lon']) ? floatval($_GET['lon']) : 0;
if ($latitude === 0 || $longitude === 0) {
echo "Please pass URL Parameters provide latitude and longitude,For example:https://m66.net/sunset.php?lat=40.7128&lon=-74.0060";
exit;
}
$sunset = getSunsetTime($latitude, $longitude);
echo "The sunset time for querying location is:$sunset";
?>
For example, visit the link:
https://m66.net/sunset.php?lat=48.8566&lon=2.3522
You can check out the sunset time in Paris today.
date_sunset() is a powerful tool built in PHP, but if it is frequently used without optimization, it can easily affect performance. By using cache reasonably (even simple file caches), the computational overhead can be greatly reduced.
To further improve performance, you can consider:
Use Redis or Memcached to replace file cache
Set a reasonable cache failure mechanism (such as clearing expired caches every day)
Generate sunset data for common cities in advance
Rational use of cache is a key step to make your website access fast and stable!