In some programs, there are some special features need to be used for timed execution, if the familiar Linux friends will say it is not easy, directly to a scheduled task crontab soon realized it? This can be done, but it must be known in advance of the specific execution time before it can be written into a scheduled task. such as two o'clock in the morning, seven o'clock in the morning, or 6:30 A.M. every day, and so on.
Sometimes, however, we cannot predict this time, and execution time is dynamically generated by the program. Then, after the dynamically generated time, some of the pieces of the program are executed, and there is no use for Linux crontab to schedule tasks, because each execution time is dynamically generated, and the scheduled task needs to know a dead time. Since you cannot use a scheduled task, you can only find the implementation method from the program itself.
In PHP, there is a sleep function, presumably meaning that the program execution encounters the sleep function while pausing for n seconds before continuing. Sleep (10) means that the program is executed from the top down, the sleep (10) statement is paused for 10 seconds, and then the execution continues. The parameter in the function parentheses is a numeric value that represents the pause time, in seconds. Take a look at the following code
usage of <?php/** * Sleep function
* Jones Blog///
output Current program timestamp
echo time ();//out:1338088780
Echo ' <br/> ';
Pause 10 seconds Sleep
(ten);
Output timestamp
echo time ();//out:1338088790
The above program execution result is
Copy Code code as follows:
Let's parse the execution, the first step is to print the current timestamp 1338088780, pause for 10 seconds, and then print the timestamp. Because the program waits 10 seconds and then prints the timestamp again, the last time stamp must be more than 10 seconds longer than the first print, and the last timestamp is 1338088790.
For the above example, we use the sleep function only once, and the sleep () function can be used without restriction in the page. Please see the following code:
usage of <?php/** * Sleep function
* Jones Taiwan Blog
///Output First timestamp echo time
(); out:1338088780
echo ' <br/> ';
Pause 10 seconds Sleep
(ten);
Output second timestamp echo time
(); out:1338088790
echo ' <br/> ';
Pause for 20 seconds for Sleep
(m);
Output third timestamp echo time
(); out:1338088810
The above program execution result is
Copy Code code as follows:
1338088780
1338088790
1338088810
The above code executes the procedure:
First, print the first time stamp 1338088780
Second, suspend for 10 seconds.
Third, print the second time stamp 1338088790, is the first time stamp plus 10 seconds after the sum
Four, pause for 20 seconds.
First, print the third time stamp 1338088810, the second time stamp 1338088790 plus 20 seconds after the sum.
There are two sleep on the page, the first time is 10 seconds, and the second time is 20 seconds. This results in a total of 30 seconds of the above examples. The sleep () function appears multiple times in the page rather than overwriting the preceding code.
So how do you combine sleep to perform dynamically generated time code? Please see the following code:
<?php
/**
* Sleep function to execute dynamically generated time period code
* Jones Taiwan Blog/
/
/Current Time
echo date (' y-m-d h:i:s '); : 2012-05-27 14:58:00
echo ' <br/> ';
Dynamic generation time range
$datetime = date (' y-m-d ') at any time before six o'clock in the afternoon to 0 o'clock the evening. ' '. Rand (' 18,23 '). ': Rand (' 0,59 '). Rand (' 0,59 '); 2012-05-27 19:20:00
//time stamp
$a = strtotime ($datetime);
Calculate time difference
$reduce = $a-time ();
Sleep Waiting for Sleep
($reduce);
code block executed after execution to time
echo date (' y-m-d h:i:s ');//Out:2012-05-27 19:20:00
Above code output:
Copy Code code as follows:
2012-05-27 14:58:00
2012-05-27 19:20:00
Parsing: Start printing the current time, then randomly calculate the program back execution time 2012-05-27 19:20:00, because the sleep accept parameter is a number in seconds, so first convert the generated time to the timestamp and then use the timestamp minus the current timestamp to draw a time difference. Then sleep can achieve the program in the random generation time to execute some statements to achieve the effect of timing execution. Note here that you must calculate a time difference in seconds, and you can't use the sleep function if you can't work out a second.
Finally, maybe some children's shoes to do when the example will say how my program execution error, prompt timeout. This problem does not panic, this is the PHP default page execution time, in PHP, the default execution page time is 30 seconds, which is sufficient for general procedures. But if you want to do a function like timed execution, you must Set_time_limit (0) The execution time of the head declaration setting. 0 is the limit when the unit is seconds. Finally, the whole post code:
<?php
/**
* Sleep function to execute dynamically generated time period code
* Jones Taiwan Blog
//
Set the page execution time, otherwise there will be timeout error prompted
set_time_limit (0);
Current Time
echo date (' y-m-d h:i:s ');//out:2012-05-27 14:58:00
//Dynamic generation time range $datetime at any time before six o'clock in the afternoon to 0 o'clock
Date (' y-m-d '). ' '. Rand (' 18,23 '). ': Rand (' 0,59 '). Rand (' 0,59 '); 2012-05-27 19:20:00
//time stamp
$a = strtotime ($datetime);
Calculate time difference
$reduce = $a-time ();
Sleep Waiting for Sleep
($reduce);
code block executed after execution to time
echo date (' y-m-d h:i:s ');//Out:2012-05-27 19:20:00