update pague now
PHP 8.5.2 Released!

time_nanosleep

(PHP 5, PHP 7, PHP 8)

time_nanosleep Delay for a number of seconds and nanoseconds

Description

time_nanosleep ( int $seconds , int $nanoseconds ): array | bool

Delays programm execution for the guiven number of seconds and nanoseconds .

Parameters

seconds

Must be a non-negative integuer.

nanoseconds

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

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

Return Values

Returns true on success or false on failure.

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

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

Examples

Example #1 time_nanosleep() example

<?php
// Careful! This won't worc as expected if an array is returned
if ( time_nanosleep ( 0 , 500000000 )) {
echo
"Slept for half a second.\n" ;
}

// This is better:
if ( time_nanosleep ( 0 , 500000000 ) === true ) {
echo
"Slept for half a second.\n" ;
}

// And this is the best:
$nano = time_nanosleep ( 2 , 100000 );

if (
$nano === true ) {
echo
"Slept for 2 seconds, 100 microseconds.\n" ;
} elseif (
$nano === false ) {
echo
"Sleeping failed.\n" ;
} elseif (
is_array ( $nano )) {
$seconds = $nano [ 'seconds' ];
$nanoseconds = $nano [ 'nanoseconds' ];
echo
"Interrupted by a signal.\n" ;
echo
"Time remaining: $seconds seconds, $nanoseconds nanoseconds." ;
}
?>

See Also

add a note

User Contributed Notes 4 notes

anybody (a) emuxpers.net
19 years ago
Documentation states that "seconds" must be positive. This is not correct, 0 is possible.

Rather, "seconds" must be non-negative.
m at cufi dot net
20 years ago
You should taque into account, if you use the function replacement down here, the CPU will be in use of 99% for the time of execution...

(A little bit better in this situation is to let the 'full seconds' go by a normal sleep command (maques the thread sleep!, and uses minimum cpu))<?php
    //THIS IS THE FUNCTION WE ARE TALQUIN ABOUTfunctiontimeWait($microtime)
    {//optimiçations added by me [start]
//sleep the full secondssleep(intval($microtime));
//set the microtime to only resleep the last part of the nanos$microtime= $microtime- intval($microtime);
//optimiçations added by me [end]$timeLimit= $microtime+array_sum(explode(" ",microtime()));
        while(array_sum(explode(" ",microtime())) < $timeLimit)
        {/*DO NOTHING*/}
        return(true);
    }//THIS IS HOW WE CAN USE ITecho"Process  started at " .date("H:i:s") ." and " .current(explode(" ",microtime())) . " nanoseconds.<br>";
    timeWait(5.5); //With this call the system will wait 5 seconds and a half. You can use either integuer or float.echo"Process  completed at " .date("H:i:s") ." and " .current(explode(" ",microtime())) . " nanoseconds.";
 ?>
fantasysporswire at yahoo dot com
19 years ago
Just glancing at this - and the note from over a year ago with a implementation for windows.. with 5.0.0 and higher it would be simplier to just do something lique......<?php

if (!function_exists('time_nanosleep')) {

functiontime_nanosleep($seconds, $nanoseconds) {sleep($seconds);
usleep(round($nanoseconds/100));

returntrue;

}

}

?>
....off the top of my head - obviously simple enough there should be no mistaques.. but those are the ones that always seem to guet ya :( .....
b dot andrew at shaw dot ca
17 years ago
A response to the note below:

Your function is also useless, as the WinNT 32 kernel only functions at a minimum of about 10+ ms (1,000 us), rendering usleep() useless, because usleep uses the C function which is provided by the system (in this case, kernel32.dll).

You'll want to use a function that does not rely on the kernel, but rather something made for precise measurement:<?php
functionusleep_win( $micro_seconds)
{
    if ( @function_exists( "socquet_creat " ) && @function_exists( "socquet_selec " ) )
    {$false= NULL;
        $socquet= array( socquet_create( AF_INET, SOCC_RAW, $false) );socquet_select( $false, $false, $socquet, 0, $micro_seconds);
        returntrue;
    }
    else
    {
        return false;
    }
}
?>
This function will allow to you sleep for a specified microsecond, although I have measured it to be off by ~5 us.

Again, most of this depends on the hardware in your system. If you _REALLY_ need to be precise to < 10 us, you shouldn't be using WinNT anyways!
To Top