In some programs, some special functions need to use timed execution, if familiar with the Linux friends will certainly say this is not easy, directly to a scheduled task crontab soon realized? This can be achieved, but it must be known in advance about the specific execution time before it can be written to the scheduled task. Like 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 generated dynamically by the program. Then some program fragments are executed after the dynamically generated time, and it is not possible to schedule tasks with Linux crontab, 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, which presumably means that the program executes when it encounters the sleep function and pauses for n seconds after execution. As sleep (10) means that the program is executed from the top down, after encountering the sleep (10) statement pause for 10 seconds, and then continue to execute down. The argument within the parentheses of the function is a numeric value that represents the pause time, in seconds. Take a look at the following code
Copy the Code code as follows:
<?php
/**
* Usage of the sleep function
* Jones Taiwan Blog
*/
Output Current program timestamp
echo Time (); out:1338088780
Echo '
';
Pause for 10 seconds
Sleep (10);
Output time stamp
echo Time (); out:1338088790
The result of the above program execution is
Copy the Code code as follows:
1338088780
1338088790
Let's parse the execution process, the first step is to print the current timestamp 1338088780, then pause for 10 seconds, and then print the timestamp. Since the program is waiting for 10 seconds and then printing the timestamp again, the last timestamp is definitely 10 seconds longer than the first time stamp, and the last timestamp is 1338088790.
In the example above, we used only one sleep function, and the sleep () function can be used without restrictions on the page. Take a look at the following code:
Copy the Code code as follows:
<?php
/**
* Usage of the sleep function
* Jones Taiwan Blog
*/
Output first time stamp
echo Time (); out:1338088780
Echo '
';
Pause for 10 seconds
Sleep (10);
Output second time stamp
echo Time (); out:1338088790
Echo '
';
Pause for 20 seconds
Sleep (20);
Output third time stamp
echo Time (); out:1338088810
The result of the above program execution is
Copy the Code code as follows: 1338088780
1338088790
1338088810
The above code executes the procedure:
First, print the first time stamp 1338088780
Second, pause 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.
Five, print the third time stamp 1338088810, is the second time stamp 1338088790 plus 20 seconds after the sum.
Two sleep appears on the page, the first time is 10 seconds, and the second 20 seconds. This results in a total of 30 seconds for the above example. The sleep () function is accumulated more than once in the page instead of overwriting the preceding code.
How does it combine sleep timing to execute dynamically generated time code? Take a look at the following code:
Copy code code as follows:
<?php
/**
* Sleep function periodically executes code that dynamically generates a time period
* Qiong Tai Blog
*/
//Current time
Echo Date (' y-m-d h:i:s ');//out:2012-05-27 14:58:00
Echo '
';
//Dynamic build time range six o'clock in the afternoon to 0 o'clock at any time today
$datetime = Date (' y-m-d '). ' '. Rand (' 18,23 '). ': '. Rand (' 0,59 '). ': '. Rand (' 0,59 '); 2012-05-27 19:20:00
//timestamp
$a = strtotime ($datetime);
//Calculate time difference
$reduce = $a-time ();
//Sleep wait
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 The code is as follows: 2012-05-27 14:58:00
2012-05-27 19:20:00
Parsing: Start printing the current time and then randomly calculate the program 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 a timestamp and then use the time stamp minus the current time stamp to get a difference, and then sleep to achieve the program at random generation time to execute some statements to achieve the timing of execution effect. Note here that there must be a time difference in seconds, and the sleep function cannot be used if the seconds difference cannot be calculated.
Finally, some children's shoes may be done when the example will say how my program execution error, prompt timeout. This problem does not panic, this is the default PHP page execution time caused by default in PHP execution page time is 30 seconds, which is sufficient for the general program. But if you want to do something like timed execution, you must then set the execution time Set_time_limit (0) in the head declaration. 0 is the representative of the time, the unit is the second. Finally, the whole code is posted:
Copy the Code code as follows:
<?php
/**
* The sleep function periodically executes code that dynamically generates time periods
* Jones Taiwan Blog
*/
Set the page execution time, otherwise there will be a timeout error prompt
Set_time_limit (0);
Current time
echo Date (' y-m-d h:i:s '); Out:2012-05-27 14:58:00
Dynamic build time range from six o'clock in the afternoon to 0 o'clock at any time of the day
$datetime = 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 the time difference
$reduce = $a-time ();
Sleep wait
Sleep ($reduce);
Block of code executed after execution to time
echo Date (' y-m-d h:i:s '); Out:2012-05-27 19:20:00