Home Backend Development PHP Tutorial PHP中使用sleep函数实现定时任务实例分享_php实例

PHP中使用sleep函数实现定时任务实例分享_php实例

Jun 07, 2016 pm 05:16 PM
php sleep function

在某些程序中,有一些特殊的功能需要用到定时执行,如果熟悉Linux的朋友肯定会说这不是容易吗,直接来个计划任务crontab不久实现了吗?这的确是可以实现,但必须是提前知道具体的执行时间,然后才能写到计划任务里去。比如凌晨两点,上午七点,或者每天上午六点三十分等等。

然而有时候,这个时间我们无法预知,而执行时间是程序动态生成的。然后在动态生成的时间后执行某些程序片段,这里就不能用Linux的crontab计划任务了,因为每次执行的时间都是动态生成,而计划任务需要知道一个定死了的时间。既然无法使用计划任务,那么就只能从程序本身寻找实现方法。

在PHP中,有一个sleep函数,大概意思是程序执行遇到sleep函数时暂停N秒后继续往下执行。如sleep(10)意思就是程序自上往下执行,遇到sleep(10)语句后暂停十秒,然后继续往下执行。函数括号内的参数是一个数值,代表暂停时间值,单位秒。请看下面一段代码

复制代码 代码如下:

/**
 * sleep函数的用法
 * 琼台博客
 */
// 输出当前程序时间戳
echo time();  // out:1338088780
echo '
';
 
// 暂停十秒
sleep(10);
 
// 输出时间戳
echo time(); // out:1338088790

以上程序执行结果是
复制代码 代码如下:

1338088780
1338088790

我们来解析下执行过程,第一步是打印当前时间戳1338088780,然后暂停十秒,再打印时间戳。由于程序是等待了十秒然后再次打印时间戳,那么就是最后一次的时间戳肯定要比第一次打印的时间戳多十秒钟,结果最后一次的时间戳是1338088790。

以上例子,我们只用了一次sleep函数,页面中可以无限制使用sleep()函数。请看以下代码:

复制代码 代码如下:

/**
 * sleep函数的用法
 * 琼台博客
 */
// 输出第一次时间戳
echo time();      // out: 1338088780
echo '
';
 
// 暂停十秒
sleep(10);
 
// 输出第二次时间戳
echo time();     // out: 1338088790
echo '
';
 
// 暂停二十秒钟
sleep(20);
 
// 输出第三次时间戳
echo time();     // out: 1338088810

以上程序执行结果是
复制代码 代码如下:
1338088780
1338088790
1338088810

以上代码执行过程:
第一,打印第一次时间戳1338088780
第二,暂停十秒
第三,打印第二次时间戳 1338088790,是第一次时间戳加上十秒后的总和
第四,暂停二十秒
第五,打印第三次时间戳 1338088810,是第二次时间戳1338088790加上二十秒后的总和。

页面中出现两次sleep,第一次是十秒,第二次二十秒。由此得出以上实例总共执行三十秒。页面中多次出现sleep()函数时为累加而不是覆盖前面的代码。

那如何结合sleep定时执行动态生成时间的代码呢?请看以下代码:

复制代码 代码如下:

/**
 * sleep函数定时执行动态生成时间段的代码
 * 琼台博客
 */
// 目前时间
echo date('Y-m-d H:i:s'); // out:2012-05-27 14:58:00
echo '
';
 
// 动态生成时间 范围在今天下午六点到晚上零点前的任意时间
$datetime = date('Y-m-d').' '.rand('18,23').':'.rand('0,59').':'.rand('0,59');  // 2012-05-27 19:20:00
 
// 算得时间戳
$a = strtotime($datetime);
 
// 算得时间差
$reduce = $a-time();
 
// sleep等待
sleep($reduce);
 
// 执行到时间后执行的代码块
echo date('Y-m-d H:i:s');  // out:2012-05-27 19:20:00

以上代码输出:
复制代码 代码如下:
2012-05-27 14:58:00
2012-05-27 19:20:00

解析:开始打印当前时间,然后随机算出程序往后执行时间 2012-05-27 19:20:00,由于sleep接受参数是一个以秒为单位的一个数值,所以先把生成的时间转换为时间戳然后再用时间戳减去当前时间戳得出一个时间差,再sleep即可达到程序在随机生成时间执行某些语句达到定时执行效果。这里注意必须算的一个以秒为单位的时间差,如果不能算出秒差就没法使用sleep函数功能。

最后,可能某些童鞋做实例的时候会说怎么我的程序执行出错,提示超时。出现这个问题不要慌,这是PHP默认页面执行时间导致的,在PHP中默认执行页面时间是三十秒钟,这对于一般程序够用了。但如果你要做类似定时执行功能就必须再头部声明设置下执行时间set_time_limit(0)。0是代表不限时,单位是秒。最后整体贴出代码:

复制代码 代码如下:

/**
 * sleep函数定时执行动态生成时间段的代码
 * 琼台博客
 */
// 设置页面执行时间,否则会有超时错误提示
set_time_limit(0);
 
// 目前时间
echo date('Y-m-d H:i:s'); // out:2012-05-27 14:58:00
 
// 动态生成时间 范围在今天下午六点到晚上零点前的任意时间
$datetime = date('Y-m-d').' '.rand('18,23').':'.rand('0,59').':'.rand('0,59');  // 2012-05-27 19:20:00
 
// 算得时间戳
$a = strtotime($datetime);
 
// 算得时间差
$reduce = $a-time();
 
// sleep等待
sleep($reduce);
 
// 执行到时间后执行的代码块
echo date('Y-m-d H:i:s');  // out:2012-05-27 19:20:00
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles