PHP Reference ยท Date and Time
PHP sleep() and usleep() functions
Reference for PHP sleep(), usleep(), time_nanosleep() and time_sleep_until(): pause a script for seconds, microseconds or nanoseconds, with return values and pitfalls.
sleep() suspends the script for a whole number of seconds. usleep() does the same in microseconds (millionths of a second), and time_nanosleep() accepts seconds plus nanoseconds. The script does nothing while it waits; the CPU is free for other processes.
Use sleeps sparingly: they are useful in command-line workers, retry loops with back-off and rate-limited API clients. In a web request a sleep holds a PHP worker for its whole duration.
sleep(2); // two seconds usleep(250_000); // a quarter of a second
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
sleep(int $seconds): int usleep(int $microseconds): void time_nanosleep(int $seconds, int $nanoseconds): array|bool time_sleep_until(float $timestamp): bool
| Name | Description |
|---|---|
$seconds |
Whole seconds to pause. Must not be negative. |
$microseconds |
Microseconds to pause. Must not be negative. |
$nanoseconds |
Extra nanoseconds, from 0 to 999,999,999. |
$timestamp |
For time_sleep_until(): the Unix time (with fractions) to wake up at. |
Return value: sleep() returns 0 on success, or the number of seconds left if the sleep was interrupted by a signal (on Windows it returns 0). usleep() returns nothing. time_nanosleep() returns true, or an array with the remaining time when interrupted, or false on failure. time_sleep_until() returns true or false (it warns when the time is already in the past).
Errors: a negative argument throws ValueError.
Usage notes
- Seconds are integers.
sleep(0.5)truncates tosleep(0)(with a deprecation notice for implicit float-to-int conversion). Useusleep(500_000)for half a second. - Precision is approximate. The wait is at least the requested time, plus scheduler delay. Do not use sleep for exact timing.
- Sleeping does not count as CPU time.
max_execution_timeon the CLI is unlimited; under a web server the limit counts only CPU time on Linux, but a real wall-clock limit (proxy, FastCGI, load balancer) still applies. - Do not sleep in web requests to simulate load or throttle users. It ties up a worker; use queues, rate limiting or a front-end delay.
- Blocking is total. Nothing else in the same PHP process runs while it sleeps (unless you use Fibers or an event loop, which schedule their own waits).
- Back-off: double the delay after each failed retry and add random jitter so many clients do not retry in lockstep.
- Signals: if a handler runs,
sleep()may return early; check the return value if you need the full delay. - Tests: avoid real sleeps in unit tests. Inject a clock or sleeper function instead.
Related
microtime()andhrtime(),time(),DateTime- Lessons: Date and time, Fibers