PHP | time_sleep_until( ) Function

The time_sleep_until() function in PHP is an inbuilt function which is used to delay execution of the current script until the specified time.
The time_sleep_until( ) function accepts timestamp as a parameter and this timestamp denotes when the script should wake.
The time_sleep_until( ) function returns TRUE on success or FALSE on failure.

Syntax:

time_sleep_until(timestamp)

Parameters Used: The time_sleep_until() function in PHP accepts one parameter timestamp . It is a mandatory parameter which specifies the time to wake.



Return Value: It returns TRUE on success or FALSE on failure.

Errors And Exceptions:

  1. If the specified timestamp is in the past, this function will generate an E_WARNING.
  2. All signals are delivered after the script wakes up.
  3. This function throws an error if the specified number is negative.

Examples:

Input : echo date('h:i:s');
        time_sleep_until(time()+5);
        echo date('h:i:s'); 
Output: 07:23:26
        07:23:31

Input : echo date('h:i:s');
        time_sleep_until(time()+ rand(1, 3));
        echo date('h:i:s');
Output : 07:21:55
         07:21:57

Below programs illustrate the time_sleep_until() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// displaying time
echo date('h:i:s');
  
// delaying execution of script for 5 seconds
time_sleep_until(time()+5);
  
// displaying time again
echo ("\n");
echo date('h:i:s'); 
?>

chevron_right


Output:

06:50:04
06:50:08

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// displaying time
echo date('h:i:s');
  
// using rand() function to randomly choose a
// value and delay execution of the script
time_sleep_until(time()+ rand(1, 3));
  
// displaying time again
echo ("\n");
echo date('h:i:s'); 
?>

chevron_right


Output:

06:50:14
06:50:15

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// delaying execution of script with negative time
time_sleep_until(time()-2);
  
// displaying time again
echo ("\n");
echo date('h:i:s'); 
?>

chevron_right


Output:

false

Reference : http://php.net/manual/en/function.time-sleep-until.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.