In PHP, we have a set of functions that do not just handle date formatting, but interact with the system itself and the script execution time. These functions are crucial for code optimization, server overload prevention, and secure file storage.
| Function | Description | Example |
|---|---|---|
sleep() |
Delay in seconds. | sleep(2); |
usleep() |
Delay in microseconds. | usleep(500000); |
hrtime() |
High-resolution time (nanoseconds). | hrtime(true); |
uniqid() |
Generates a unique ID string. | uniqid("img_"); |
time_sleep_until() |
Sleep until a specified timestamp. | time_sleep_until($t); |
The sleep() and usleep() functions act as a "pause" for your code. They are most
commonly used in automated tasks, such as sending emails, where we want to avoid overloading the server.
hrtime() (High Resolution Time) uses the system clock and returns time in nanoseconds. This is
essential for pinpointing which part of your code is slow.
uniqid() handles unique file naming or session identifiers based on the current time in
microseconds, which prevents duplicate names.