gettimeofday()
Learn the PHP gettimeofday() function: syntax, return values, the as_float parameter, removal in PHP 8.0, and how to replace it with microtime(true).
The PHP gettimeofday() Function
The gettimeofday() function returns the current system time. By default it returns an associative array with the seconds and microseconds since the Unix epoch plus timezone details. With its optional argument set to true, it returns the time as a single float instead.
Important:
gettimeofday()was removed in PHP 8.0.0. If you are writing or maintaining modern PHP, usemicrotime(true)for high-precision timestamps. This chapter explains the legacy function and shows the recommended replacement so you can migrate older code with confidence.
This page covers the syntax, the optional parameter, the exact return value, runnable examples, and what to use instead.
Syntax
gettimeofday(bool $as_float = false): array|floatParameters
| Parameter | Type | Description |
|---|---|---|
$as_float | bool | Optional. When true, return a single float (seconds with microseconds). When omitted or false, return an associative array. Defaults to false. |
Return Value
When $as_float is false (the default), gettimeofday() returns an associative array with these keys:
sec— seconds since the Unix epoch (int).usec— microseconds within the current second (int).minuteswest— minutes west of Greenwich (int).dsttime— type of daylight-saving-time correction (int).
When $as_float is true, it returns a single float: the number of seconds since the epoch, with the microseconds expressed as the fractional part.
Example: the default array form
<?php
$now = gettimeofday();
print_r($now);A typical result looks like this (the numbers depend on the moment you run it):
Array
(
[sec] => 1718928000
[usec] => 512340
[minuteswest] => 0
[dsttime] => 0
)Example: the float form
Passing true returns one value you can use directly in arithmetic — handy for measuring elapsed time:
<?php
$float = gettimeofday(true);
echo $float; // e.g. 1718928000.51234Recommended replacement: microtime(true)
Because gettimeofday() no longer exists in PHP 8.0+, the portable way to get a high-precision timestamp is microtime(true), which returns the same kind of float:
<?php
$start = microtime(true);
// ... code you want to time ...
for ($i = 0; $i < 1_000_000; $i++) {
// busy work
}
$elapsed = microtime(true) - $start;
echo "Elapsed: " . round($elapsed, 6) . " seconds";If you specifically need the seconds/microseconds split that the old array gave you, you can rebuild it from microtime(true):
<?php
$t = microtime(true);
$sec = (int) $t;
$usec = (int) round(($t - $sec) * 1_000_000);
echo "sec={$sec}, usec={$usec}";When would I use this?
You will mostly meet gettimeofday() while reading or upgrading legacy code written for PHP 5 or 7. Its real-world uses were:
- High-precision timing and profiling (how long a block of code or a database query takes).
- Generating timestamps with sub-second resolution for logs.
For all of these, modern PHP code should use microtime(true). For whole-second timestamps where microseconds do not matter, time() is simpler, and getdate() returns a labelled array of date parts. To format any of these into a human-readable string, see the date() function.
Performance Considerations
gettimeofday() is a system call, so it has a small overhead. More importantly, calling it in PHP 8.0+ raises an Error because the function was removed. microtime(true) is supported across all current PHP versions, is lightweight, and returns a float ready for arithmetic — making it the correct choice for new code.
Conclusion
gettimeofday() returned the current time either as an associative array (sec, usec, minuteswest, dsttime) or, with $as_float set to true, as a single float. It was removed in PHP 8.0.0, so for accurate microsecond timestamps and profiling in modern PHP, use microtime(true) instead.