PHP Function Reference

PHP time_sleep_until() Function



The PHP time_sleep_until() function is used to make the script sleep until the specified timestamp.

Syntax

time_sleep_until(timestamp)

Parameters

timestamp Required. Specify the timestamp when the script should wake.

Return Value

Returns true on success or false on failure.

Exceptions

Generates an E_WARNING if the specified timestamp is in the past.

Example: time_sleep_until() example

The example below shows the usage of time_sleep_until() function.

<?php
//displaying the current time
echo date('h:i:s')."\n";

//getting current time measured in the number
//of seconds since the Unix Epoch
$curr_timestamp = time();

//sleep for 2 seconds
time_sleep_until($curr_timestamp + 2);

//wake up and displaying the current time
echo date('h:i:s')."\n";
?>

The output of the above code will be similar to:

06:50:13
06:50:15

Example: using past timestamp

When a past timestamp is used with this function, it generates an E_WARNING. Consider the example below:

<?php
//returns false and generates a warning
var_dump(time_sleep_until(time()-1));
?>

The output of the above code will be similar to:

bool(false)

PHP Warning:  time_sleep_until(): Argument #1 ($timestamp) must be greater than or equal to the current time in Main.php on line 3

❮ PHP Miscellaneous Reference