Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Miscellaneous Functions : time_nanosleep

time_nanosleep

Delay for a number of seconds and nanoseconds (PHP 5)
mixed time_nanosleep ( int seconds, int nanoseconds )

Example 1364. 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.";
}
?>

Code Examples / Notes » time_nanosleep

m

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.
";
   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

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.
";
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.";
?>


fantasysportswire

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 like......
<?
if (!function_exists('time_nanosleep')) {
function time_nanosleep($seconds, $nanoseconds) {
sleep($seconds);
usleep(round($nanoseconds/100));
return true;
}
}
?>
....off the top of my head - obviously simple enough there should be no mistakes.. but those are the ones that always seem to get ya :( .....


anybody a emuxperts.net

Documentation states that "seconds" must be positive. This is not correct, 0 is possible.
Rather, "seconds" must be non-negative.


Change Language


Follow Navioo On Twitter
connection_aborted
connection_status
connection_timeout
constant
define
defined
die
eval
exit
get_browser
__halt_compiler
highlight_file
highlight_string
ignore_user_abort
pack
php_check_syntax
php_strip_whitespace
show_source
sleep
sys_getloadavg
time_nanosleep
time_sleep_until
uniqid
unpack
usleep
eXTReMe Tracker