time_nanosleep

(PHP 5)

time_nanosleep --  Delay for a number of seconds and nanoseconds

说明

mixed time_nanosleep ( int seconds, int nanoseconds )

Delays program execution for the given number of seconds and nanoseconds.

seconds must be a positive integer, and nanoseconds must be a positive integer less than 1 billion.

返回值

如果成功则返回 TRUE,失败则返回 FALSE

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

范例

例子 1. time_nanosleep() example

<?php
// Careful! This won't work 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 milliseconds.\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.";
}
?>

注: 本函数未在 Windows 平台下实现。


add a note add a note User Contributed Notes
anybody (a) emuxperts.net
22-Aug-2006 07:03
Documentation states that "seconds" must be positive. This is not correct, 0 is possible.

Rather, "seconds" must be non-negative.
m at kufi dot net
14-Aug-2005 05:03
You should take 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 (makes the thread sleep!, and uses minimum cpu))

<?php
  
//THIS IS THE FUNCTION WE ARE TALKIN ABOUT
  
function timeWait($microtime)
   {
//optimizations added by me [start]
//sleep the full seconds
sleep(intval($microtime));
//set the microtime to only resleep the last part of the nanos
$microtime = $microtime - intval($microtime);
//optimizations 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 IT
  
echo "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 integer or float.
  
echo "Process completed at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.";
 
?>
tecnomaniac at ig dot com dot br
27-Jul-2005 06:04
This is an alternative function to sleep_nanosecond that you can use with PHP versions below PHP 5.0. It is not very accurate if we talk about nanoseconds but the results are satisfatory. Enjoy!

<?php
  
//THIS IS THE FUNCTION WE ARE TALKIN ABOUT
  
function timeWait($microtime)
   {
      
$timeLimit = $microtime + array_sum(explode(" ",microtime()));
       while(
array_sum(explode(" ",microtime())) < $timeLimit)
       {
/*DO NOTHING*/}
       return(
true);
   }

  
//THIS IS HOW WE CAN USE IT
  
echo "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 integer or float.
  
echo "Process completed at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.";
?>