How to combine cron timing scripts to regularly push the sunset time obtained using PHP's date_sunset every day?
When developing some time-based push systems, we often use timing tasks to automatically perform some operations. For example, we can regularly obtain the sunset time of a certain area and push it to the specified interface or user. This article will use PHP's date_sunset function combined with cron timing tasks to realize the function of pushing sunset time regularly every day.
First, we need to use PHP's date_sunset function to get the sunset time. This function takes parameters such as latitude, longitude, and timestamp, and returns sunset time.
<?php
// Set the time zone
date_default_timezone_set('Asia/Shanghai');
// Define latitude and longitude
$latitude = 39.9042; // Beijing's latitude
$longitude = 116.4074; // Longitude in Beijing
// Get the sunset time(by Unix Timestamp returns)
$sunset = date_sunset(time(), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
// Format the sunset time into a readable format
$sunset_time = date("Y-m-d H:i:s", $sunset);
echo "Today's sunset time: " . $sunset_time;
?>
We want to push the sunset time regularly every day, so we need to use the cron timed task to execute this PHP script.
We already have PHP code to get the sunset time, and we need to write this code to a PHP script, such as send_sunset_time.php . You can add push code to the script to push the sunset time to the specified URL.
<?php
// Set the time zone
date_default_timezone_set('Asia/Shanghai');
// Define latitude and longitude
$latitude = 39.9042;
$longitude = 116.4074;
// Get the sunset time
$sunset = date_sunset(time(), SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
$sunset_time = date("Y-m-d H:i:s", $sunset);
// Push sunset time to the specified URL(Assume that API interface)
$api_url = 'https://m66.net/api/send_sunset_time'; // Replace with your actual URL
$data = array(
'sunset_time' => $sunset_time
);
// use cURL send POST ask
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
if ($response) {
echo "Sunset time has been successfully pushed: " . $sunset_time;
} else {
echo "Push failed";
}
?>
cron is a timed task scheduler under Linux system. We can set the PHP scripts to execute every day by editing crontab .
Open the crontab file:
crontab -e
Add a line to set the script to execute at a specified time every day (for example, every day at 6 pm):
0 18 * * * /usr/bin/php /path/to/your/script/send_sunset_time.php
The meaning of this line of command is: execute the send_sunset_time.php script at 18:00 every day.
Save and exit the crontab file, and the timed task will take effect.
Through the above steps, we have implemented the use of PHP to obtain the sunset time and push this time to the specified URL through the cron timing task.
Each time the system executes, the system will automatically get the sunset time of the current date and push it to the URL (in the above code, the URL has been replaced with m66.net ). This can help us regularly push the sunset time of the day to the relevant system.
Through PHP's date_sunset function, we can easily obtain the sunset time. Combined with the cron timing task, we can push this information at a fixed time every day. This method can be widely used in time-based push systems, such as weather forecasts, sunrise and sunset reminders, etc.