The example in this article describes how to implement scheduled tasks in PHP. The code is simple and practical. Share it with everyone for your reference.
The specific implementation method is as follows:
ignore_user_abort(true); //客户端断开时忽略脚本中止(允许脚本一直执行) set_time_limit(0); //设置脚本最长执行时间,0不限制 do{ $handle = fopen('auto.txt', 'w'); if($fp) { $text = '你好\n\r'; $count = 0; for($i=1; $i<10; $i++) { if(! $c = fwrite($handle, '第'.$i.'行:'.$text)) //返回写入字符数,失败时返回false { echo '第'.$i.'次的写入失败!'; } $count += $c; } } fclose($handle); sleep(60); //延缓60秒执行 }while(true);
I hope this article will be helpful to everyone’s PHP programming design.
If you want to use PHP files, you can barely do it
There is a sleep function in php. The specific use is sleep(time) where time is in seconds
First, create a php script
php
while(1){
//yourcode
sleep(3600*24);
}
?>
The yourcode here is the function you want the PHP script to execute. Although this goal can be achieved, there are sacrifices. You have to access this script through the URL at 8 o'clock in the morning, which means executing the PHP file, and this link cannot be interrupted and must continue! ! Otherwise, it will be invalid
PHP is executed only upon request, and is executed only when there is a request.
ignore_user_abort(); // Run in the background
set_time_limit(0); // Run continuously
$interval=30; // Set the timing value
do {
//Do what you want to do
sleep($interval); // Wait for the waiter
}while(true);
?>