Menu Home

time_nanosleep | PHP | Functions | time_nanosleep

Delay for a number of seconds and nanoseconds


time_nanosleep ( int $seconds , int $nanoseconds ) : mixed



Must be a non-negative integer.


Must be a non-negative integer less than 1 billion.

Note: On Windows, the system may sleep longer that the given number of nanoseconds, depending on the hardware.


Returns TRUE on success or FALSE on failure.

If the delay was interrupted by a signal, an associative array will be returned with the components:

seconds number of seconds remaining in the delay
nanoseconds number of nanoseconds remaining in the delay



// won't work as expected if an array is returned
if (time_nanosleep(0, 500000000)) {
    echo "slept 0.5 seconds\n";

// better
if (time_nanosleep(0, 500000000) === true) {
    echo "slept 0.5 seconds\n";

// best
$sleep = time_nanosleep(0, 500000000);
if ($sleep === true) {
    echo "slept 0.5 seconds\n";
} elseif ($sleep === false) {
    echo "sleep failed\n";
} elseif (is_array($sleep)) {
    $seconds = $sleep['seconds'];
    $nanoseconds = $sleep['nanoseconds'];
    echo "interrupted by signal\n";
    echo "time remaining: $seconds seconds and $nanoseconds nanoseconds";

slept 0.5 seconds
slept 0.5 seconds
slept 0.5 seconds