time_nanosleep / time_sleep_until
time_nanosleep(int $seconds, int $nanoseconds): array|bool; time_sleep_until(float $timestamp): bool
The sleep family closure: time_nanosleep(seconds, nanoseconds) takes dual-integer args for precise delay (verified against the current php.net manual as NOT deprecated - a live page from PHP 5 through 8); a signal interrupt returns an array of remaining seconds/nanoseconds, so compare strictly with === true; time_sleep_until(timestamp) sleeps until a fixed moment, returning false with an E_WARNING for a past timestamp. Together with the sleep/usleep/hrtime family it forms a complete system.
C C23
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
// C: nanosleep(2) takes a struct timespec {seconds, nanoseconds} -
// the POSIX twin of time_nanosleep (interrupted sleeps fill the remainder struct)
int main(void) {
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
struct timespec req = {0, 50000000L}; /* 50 ms = 0 s + 50,000,000 ns */
bool ok = nanosleep(&req, NULL) == 0;
clock_gettime(CLOCK_MONOTONIC, &t1);
double elapsed = (double)(t1.tv_sec - t0.tv_sec) + (double)(t1.tv_nsec - t0.tv_nsec) / 1e9;
printf("%s\n", ok ? "true" : "false");
printf("%s\n", elapsed > 0.04 ? "slept at least ~40ms" : "unexpected");
return 0;
}
true slept at least ~40ms
C++ C++26
#include <chrono>
#include <cstdio>
#include <thread>
// C++11: std::this_thread::sleep_for(duration) - the family equivalent
int main() {
auto t0 = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
auto t1 = std::chrono::steady_clock::now();
double ms = std::chrono::duration<double, std::milli>(t1 - t0).count();
std::printf("%s\n", ms > 40 ? "slept at least ~40ms" : "unexpected");
return 0;
}
slept at least ~40ms
C# v11.0.100
// C#: Task.Delay is the async twin - non-blocking for the caller thread
using System.Diagnostics;
var sw = Stopwatch.StartNew();
await Task.Delay(50);
Console.WriteLine(sw.ElapsedMilliseconds > 40 ? "slept at least ~40ms" : "unexpected");
slept at least ~40ms
Go v1.27.4
package main
import (
"fmt"
"time"
)
// Go: time.Sleep blocks the current goroutine for a Duration
func main() {
t0 := time.Now()
time.Sleep(50 * time.Millisecond)
fmt.Println(time.Since(t0) > 40*time.Millisecond)
}
true
Java v27.0.1
public class Main {
public static void main(String[] args) throws InterruptedException {
// Java: Thread.sleep(milliseconds) - whole-ms precision, throws InterruptedException
long t0 = System.nanoTime();
Thread.sleep(50);
long ms = (System.nanoTime() - t0) / 1_000_000;
System.out.println(ms > 40 ? "slept at least ~40ms" : "unexpected");
}
}
slept at least ~40ms
JavaScript v26.13.1
// JS: no blocking sleep in the language core - setTimeout schedules a callback
// on the event loop; the callback form below keeps the process alive until it fires
const t0 = Date.now();
setTimeout(() => {
console.log(Date.now() - t0 > 40 ? "slept at least ~40ms" : "unexpected");
}, 50);
slept at least ~40ms
PHP v8.6.0
// time_nanosleep($seconds, $nanoseconds) + time_sleep_until($timestamp): precision sleep family
$t0 = microtime(true);
var_dump(time_nanosleep(0, 50000000) === true); // 50 ms; signal interrupt returns an ARRAY, so use === true
var_dump((microtime(true) - $t0) > 0.04);
var_dump(time_sleep_until(time() - 1) === false); // past time: false + E_WARNING (suppressed in captured output)
$t1 = microtime(true);
var_dump(time_sleep_until($t1 + 0.05) === true);
var_dump((microtime(true) - $t1) > 0.04);
bool(true) bool(true) bool(true) bool(true) bool(true)
Python v3.15.2
# Python: time.sleep takes float seconds - the float twin of the family
import time
t0 = time.monotonic()
time.sleep(0.05) # 50 ms
print(time.monotonic() - t0 > 0.04)
True
Rust v1.99.1
use std::thread;
use std::time::{Duration, Instant};
// Rust: thread::sleep(Duration) blocks the current thread
fn main() {
let t0 = Instant::now();
thread::sleep(Duration::from_millis(50));
println!("{}", t0.elapsed() > Duration::from_millis(40));
}
true
TypeScript v26.13.1
// TS: same runtime as JS; the delay typed in milliseconds
const t0: number = Date.now();
setTimeout(() => {
console.log(Date.now() - t0 > 40 ? "slept at least ~40ms" : "unexpected");
}, 50);
slept at least ~40ms
Language differences
C: the direct analogue is POSIX nanosleep(2) - it likewise takes two quantities (struct fields: seconds + nanoseconds) and writes the remaining time into a remainder struct on interruption, isomorphic to PHP's returned array.
C++: std::this_thread::sleep_for (C++11), taking any chrono duration.
C#: Thread.Sleep blocks the thread; Task.Delay is the non-blocking async twin (shown in the demo).
Go: time.Sleep(d) blocks the current goroutine; there is no time.SleepUntil - use a time.Timer to sleep until a fixed moment.
Java: Thread.sleep(milliseconds) - whole-ms granularity only; sleeping until a fixed moment requires computing the difference yourself.
JavaScript: the language core has NO blocking sleep - setTimeout schedules a callback on the event loop, and the Promise wrapper (await new Promise(r => setTimeout(r, ms))) is the async-code equivalent; Atomics.wait is the only synchronous hack (Worker-only).
PHP: time_nanosleep verified against the current php.net manual as NOT deprecated (a live page from PHP 5 through 8, no deprecation marker) - dual-integer args "seconds + nanoseconds" (nanoseconds must be < 1 billion); the return value is three-state - true on success, false on failure, and an ARRAY with remaining seconds/nanoseconds when interrupted by a signal (the manual's own example stresses comparing strictly with === true); time_sleep_until sleeps until a float timestamp and returns false with an E_WARNING for a past timestamp (error display suppressed in the captured output per validator convention); on Windows the actual sleep may exceed the given nanoseconds (manual note); family lineage: sleep (whole seconds) -> usleep (microseconds) -> time_nanosleep (seconds+nanoseconds) -> hrtime (timing, not sleeping) -> the two functions of this entry close the family.
Python: time.sleep(float seconds) - one function covers whole seconds down to sub-milliseconds.
Rust: thread::sleep(Duration); sleeping until a fixed moment uses a Duration difference.
TypeScript: same as JavaScript.
- Family conclusion: blocking sleep is the smallest primitive shared by all languages; the differences are only in parameter shape (seconds+nanoseconds dual ints vs a single duration object vs float seconds) and whether blocking is even allowed (not in the JS language core).