Use the sleep function in PHP to implement scheduled task instance sharing _ php instance

Source: Internet
Author: User
Tags echo date sleep function
This article mainly introduces how to use the sleep function to implement scheduled task instances in PHP. This article provides multiple examples of using sleep. This article can also be used as a tutorial to learn the sleep function, if you need some special functions, you can refer to some programs that require scheduled execution. If you are familiar with Linux, will it be easy, did I implement the crontab directly for a scheduled task? This is indeed feasible, but you must know the specific execution time in advance before writing it to the scheduled task. For example, seven o'clock A.M., or 06:30 A.M. every day.

However, sometimes we cannot predict this time, and the execution time is dynamically generated by the program. Then execute some program fragments after the dynamic generation time. Here, the Linux crontab task cannot be used to schedule tasks, because each execution time is dynamically generated, A scheduled task requires a fixed time. Since you cannot use scheduled tasks, you can only find implementation methods from the program itself.

In PHP, there is a sleep function, which probably means that when a program runs a sleep function, it is paused for N seconds and continues to be executed. For example, sleep (10) means that the program runs from top to bottom. When the sleep (10) Statement is run, it is paused for 10 seconds and then continues to run. The parameter in the function brackets is a numerical value, indicating the pause time value, in seconds. See the following code.

The Code is as follows:


<? Php
/**
* Sleep function usage
* Qiongtai blog
*/
// Output the timestamp of the current program
Echo time (); // out: 1338088780
Echo'
';

// Pause for 10 seconds
Sleep (10 );

// Output Timestamp
Echo time (); // out: 1338088790


The execution result of the above program is

The Code is as follows:


1338088780
1338088790


Let's parse the execution process. The first step is to print the current timestamp 1338088780, pause for 10 seconds, and then print the timestamp. Because the program waits for 10 seconds and then prints the timestamp again, the last timestamp must be 10 seconds more than the first timestamp, And the last timestamp of the result is 1338088790.

In the above example, we only use the sleep function once, and the sleep () function can be used unlimited on the page. See the following code:

The Code is as follows:


<? Php
/**
* Sleep function usage
* Qiongtai blog
*/
// Output the first Timestamp
Echo time (); // out: 1338088780
Echo'
';

// Pause for 10 seconds
Sleep (10 );

// Output the second Timestamp
Echo time (); // out: 1338088790
Echo'
';

// Pause for 20 seconds
Sleep (20 );

// Output the third Timestamp
Echo time (); // out: 1338088810


The execution result of the above program is

The Code is as follows:

1338088780
1338088790
1338088810


The above Code Execution Process:
First, print the first timestamp 1338088780.
Second, pause for 10 seconds
Third, print the second timestamp 1338088790, which is the sum after the first timestamp plus ten seconds.
Fourth, pause for 20 seconds
Fifth, print the third timestamp 1338088810, which is the sum of the second timestamp 1338088790 plus 20 seconds.

Two sleep times are displayed on the page. The first sleep is 10 seconds, and the second sleep is 20 seconds. It is concluded that the above instance is executed for a total of 30 seconds. When the sleep () function appears multiple times on the page, it accumulates rather than overwrites the previous code.

Then, how can we use sleep to regularly execute the code for the dynamic generation time? See the following code:

The Code is as follows:


<? Php
/**
* The sleep function regularly executes the code for the dynamically generated time period.
* Qiongtai blog
*/
// Current time
Echo date ('Y-m-d H: I: s'); // out: 14:58:00
Echo'
';

// Dynamic generation time range: Any time from six o'clock P.M.
$ Datetime = date ('Y-m-d '). ''. rand ('18, 23 '). ':'. rand ('0, 59 '). ':'. rand ('0, 59'); // 19:20:00

// Calculate the timestamp
$ A = strtotime ($ datetime );

// Calculate the time difference
$ Reduce = $ a-time ();

// Sleep waiting
Sleep ($ reduce );

// Code block executed after execution time
Echo date ('Y-m-d H: I: s'); // out: 19:20:00


The above code output:

The Code is as follows:

2012-05-27 14:58:00
2012-05-27 19:20:00


Resolution: Start to print the current time, and then randomly calculate the program's subsequent execution time 19:20:00, because the sleep accept parameter is a value in seconds, therefore, first convert the generated time to the timestamp and then use the timestamp minus the current timestamp to get a time difference. Then sleep can achieve the scheduled execution of some statements executed by the program at the random generation time. Note that a time difference in seconds must be calculated. If the second difference cannot be calculated, the sleep function cannot be used.

Finally, some children's shoes may say how to execute an error and prompt timeout. Don't be alarmed when this problem occurs. This is caused by the default page execution time of PHP. in PHP, the default page execution time is thirty seconds, which is enough for general programs. However, if you want to perform a scheduled execution function like this, you must declare the execution time in the header and set set_time_limit (0 ). 0 indicates the time limit, in seconds. Finally, the Code is pasted as follows:

The Code is as follows:


<? Php
/**
* The sleep function regularly executes the code for the dynamically generated time period.
* Qiongtai blog
*/
// Set the page execution time. Otherwise, a timeout error is prompted.
Set_time_limit (0 );

// Current time
Echo date ('Y-m-d H: I: s'); // out: 14:58:00

// Dynamic generation time range: Any time from six o'clock P.M.
$ Datetime = date ('Y-m-d '). ''. rand ('18, 23 '). ':'. rand ('0, 59 '). ':'. rand ('0, 59'); // 19:20:00

// Calculate the timestamp
$ A = strtotime ($ datetime );

// Calculate the time difference
$ Reduce = $ a-time ();

// Sleep waiting
Sleep ($ reduce );

// Code block executed after execution time
Echo date ('Y-m-d H: I: s'); // out: 19:20:00

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.