PHP time_nanosleep() Function

What is PHP time_nanosleep() Function?

If you want to make your code delay for a very precise timing like nanoseconds (1 second = 1,000,000,000 nanoseconds), use PHP time_nanosleep() function. The time_nanosleep() function pauses for a specified seconds and nanoseconds.

Syntax:

time_nanosleep(seconds, nanoseconds)

Parameters:

seconds (Required): It must be a non-negative integer.

nanoseconds (Required): It must be a non-negative integer. Its value must also be less than 1 billion (<1,000,000,000). Note: On Windows system, depending on different types of hardware, the system sleeps longer.

Return Values:

The function returns-

  • TRUE – on success.
  • FALSE – on failure.

If the delay is 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

Check example 1.

Examples:

Example 1:

<?php
$pause_time = 500000000;
echo "Script starts on: ". (new DateTime('now'))->format('h:i:s.u') . "<br />";
$return_val = time_nanosleep(0, $pause_time);

if ($return_val === TRUE) {
    echo "It pauses for " . $pause_time . " nanoseconds." . "<br />";
    echo "And, it completes on: " . (new DateTime('now'))->format('h:i:s.u') . "<br />";
} elseif (is_array($return_val)) {
    $seconds = $return_val['seconds'];
    $nanoseconds = $return_val['nanoseconds'];
    echo "time_nanosleep() function was Interrupted by a signal." . "<br />";
    echo "Time remaining: $seconds seconds, $nanoseconds nanoseconds." . "<br />";
}
?>

Output:

Script starts on: 06:45:31.472501
It pauses for 500000000 nanoseconds.
And, it completes on: 06:45:31.973952

Explanation:

The time_nanosleep() function pauses the script execution for 500000000 nanosecond (.5 second) on line 4. The script was paused for (31.973952 – 31.472501) = 0.501451000 nanoseconds = .501 seconds. If the time_nanosleep() function failed for signal interruption, it returns an array and the elseif code () executes.

Example 2:

<?php
$pause_time = 500000000;
echo "Script starts on: ". (new DateTime('now'))->format('h:i:s.u') . "<br />";
echo "It pauses for " . $pause_time . " nanoseconds." . "<br />";
echo "And, it completes on: " . (new DateTime('now'))->format('h:i:s.u') . "<br /><br />";

$pause_time = 500000;
echo "Script starts on: ". (new DateTime('now'))->format('h:i:s.u') . "<br />";
usleep($pause_time);
echo "It pauses for " . $pause_time . " microseconds." . "<br />";
echo "And, it completes on: " . (new DateTime('now'))->format('h:i:s.u') . "<br />";
?>

Output:

Script starts on: 03:28:41.460098
It pauses for 500000000 microseconds.
And, it completes on: 03:28:41.966375

Script starts on: 03:28:41.966398
It pauses for 500000000 microseconds.
And, it completes on: 03:28:42.482360

Explanation:

To make a pause for half a second (.5 second), time_nanosleep() function pauses for (41.966375-41.460098) = 506277000 nanoseconds = .506 seconds and usleep() function pauses for (2.482360-41.966398) = 515962 microseconds = .515 seconds pause.

Practical Usages of PHP time_nanosleep() function():

To get more precise time gap, time_nanosleep() function works better than usleep() function. See example 2. To make a pause for half a second (.5 second), time_nanosleep() function generates .506 seconds pause and usleep() function provides .515 seconds pause.

Caution:

The function works very preciously. Hence, it could be very expensive in operation. So, avoid calling it too frequently and instead use usleep() function if usleep() serves your purpose.

PHP Version Support:

PHP 5, PHP 7, PHP8

Summary: PHP time_nanosleep () Function

time_nanosleep() Function is one of the misc, type functions in PHP. This is very useful when you need very precise time delay.

Reference:

https://www.php.net/manual/en/function.time-nanosleep.php