Home  >  Article  >  php教程  >  PHP定时执行任务的实现程序

PHP定时执行任务的实现程序

WBOY
WBOYOriginal
2016-05-25 16:43:581402browse

在php中定时执行任务我们会用到函数,ignore_user_abort(),set_time_limit(0),sleep($interval) 这些函数组成的定时执行任务只要在浏览器执行一次,关了浏览器也不会有影响的.

利用php自带函数,代码如下:

ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. 
set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 
$interval=60*30;// 每隔半小时运行 
do{ 
    //这里是你要执行的代码  
    sleep($interval);// 等待5分钟 
}while(true); 
ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. 
set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 
$interval=60*30;// 每隔半小时运行

windows 的计划任务

1,写一个PHP程序,命名为test.php,内容如下所示:

<?php 
$fp = fopen("test.txt", "a+"); 
fwrite($fp, date("Y-m-d H:i:s") . " 成功成功了!n"); 
fclose($fp); 
?>

2,新建Bat文件,命名为test.bat,内容如下所示:

1 D:/php/php.exe -q D:/website/test.php //相应目录自己改上

3、建立WINDOWS计划任务:

开始—>控制面板—>任务计划—>添加任务计划

浏览文件夹选择上面的bat文件设置时间和密码(登陆WINDOWS的)保存即可了.


文章网址:

随意转载^^但请附上教程地址。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn