Sunday, May 24, 2026
HomeLanguagesPHP | time_nanosleep( ) Function

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:




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


Output:

Execution delayed for two and  half a second

Program 2:




<?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'); 
?>


Output:

06:45:15
06:45:18

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

RELATED ARTICLES

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12107 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS