PHP | time_nanosleep( ) Function

The time_nanosleep() function in PHP is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds and nanoseconds.

The time_nanosleep() function accepts seconds and nanoseconds as parameters and returns TRUE on success or FALSE on failure.

If the delay is interrupted by a signal, an associative array is returned with the following components:



  1. seconds: It denotes the number of seconds remaining in the delay.
  2. nanoseconds: It denotes the number of nanoseconds remaining in the delay.

Syntax:

time_nanosleep(seconds, nanoseconds)

Parameters Used:
The time_nanosleep() function in PHP accepts two parameters.

  • seconds : It is a mandatory parameter which specifies the number of seconds.
  • nanoseconds : It is a mandatory parameter which specifies the number of nanoseconds.

Return Value: It returns TRUE on success or FALSE on failure.If the delay is interrupted by a signal, an associative array is returned with the remaining seconds and nanoseconds.

Errors And Exceptions:

  1. The value of nanoseconds passed as parameter must be less than 1, 000, 000, 000.
  2. The value of seconds passed as parameter should be non-negative.

Below programs illustrate the time_nanosleep() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// displaying time
if (time_nanosleep(2, 500000000) === true)
{
   echo "Execution delayed for two and half a second";
}
else 
{
   echo "No delay in Execution";
}
?>

chevron_right


Output:

Execution delayed for two and  half a second

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// displaying time
echo date('h:i:s');
  
// delaying execution of the script for 2 seconds and half a second
time_nanosleep(2, 500000000);
  
// displaying time again
echo ("\n");
echo date('h:i:s'); 
?>

chevron_right


Output:

06:45:15
06:45:18

Reference : http://php.net/manual/en/function.time-nanosleep.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.