gettimeofday
Returns the current time with microsecond precision, as an array or as a float.
gettimeofday(bool $as_float = false): array|float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$as_floatoptional | bool= false | Return the time as a single float instead of an array. |
Return value
array|floatBy default an array with the keys sec, usec, minuteswest and dsttime. When as_float is true, a float holding the seconds since the Unix epoch with microsecond precision.
Errors
Does not throw.
Changelog
- PHP 5.1.0The as_float parameter was added. Before that the function always returned an array.
minuteswest and dsttime are named after the obsolete struct timezone of the C call, but PHP does not read them from the system — it derives them from its own default timezone. minuteswest is the offset west of UTC in minutes, so it is negative for zones east of UTC (UTC gives 0, America/New_York in summer gives 240, Asia/Kolkata gives -330), and dsttime is 1 while daylight saving is in effect. They therefore tell you nothing that date_default_timezone_get or a DateTimeZone would not tell you more clearly.Example
When to use it
- Writing log timestamps with a fixed six-digit microsecond field
- Generating a time-ordered request identifier from seconds and microseconds
- Recording the exact wall-clock moment an event occurred, at sub-second resolution
Frequently asked questions
What is the difference between gettimeofday and microtime?
gettimeofday(true) and microtime(true) return the identical float. The difference is only in the default: gettimeofday returns an array of separate fields, microtime returns a "usec sec" string.
Can I use gettimeofday to measure elapsed time?
You should not. It reads the wall clock, which can be stepped by NTP, so intervals can be wrong or negative — use hrtime, which reads a monotonic clock.
More examples
A log timestamp with microseconds
Keeping usec separate makes zero-padding to exactly six digits trivial. The two lines share a whole second but differ in the fractional part.
<?php
date_default_timezone_set('UTC');
function stamp(): string {
$t = gettimeofday();
return sprintf('%s.%06dZ', gmdate('Y-m-d\TH:i:s', $t['sec']), $t['usec']);
}
echo stamp(), " application started\n";
for ($i = 0; $i < 50000; $i++) { $x = sqrt($i); }
echo stamp(), " warm-up finished\n";gettimeofday(true) and microtime(true) are the same thing
Both read the same wall clock and return seconds since the epoch as a float, so the difference is only the time taken to run the second call. Pick whichever reads better in your code.
<?php
$a = gettimeofday(true);
$b = microtime(true);
printf("gettimeofday: %.6f\n", $a);
printf("microtime: %.6f\n", $b);
printf("difference: %.6f seconds\n", abs($b - $a));
echo 'both floats: ', var_export(is_float($a) && is_float($b), true), "\n";Why it is wrong for benchmarking
Both report a similar duration here, but only the monotonic reading is guaranteed. If NTP adjusts the clock mid-measurement the wall-clock figure can jump or even go negative.
<?php
$wallStart = gettimeofday(true);
$monoStart = hrtime(true);
$total = 0;
for ($i = 0; $i < 300000; $i++) { $total += $i; }
printf("wall clock: %.3f ms\n", (gettimeofday(true) - $wallStart) * 1000);
printf("monotonic: %.3f ms\n", (hrtime(true) - $monoStart) / 1e6);
echo 'sum: ', $total, "\n";
Discussion