Open In App

PHP | time_sleep_until( ) Function

Last Updated : 21 Jun, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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:




<?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'); 
?>


Output:

06:50:04
06:50:08

Program 2:




<?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'); 
?>


Output:

06:50:14
06:50:15

Program 3:




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


Output:

false

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads