microtime
Returns the current Unix timestamp including microseconds, as a string or a float.
microtime(bool $as_float = false): string|float- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$as_floatoptional | bool= false | When true, returns a single float of seconds since the Unix epoch instead of the two-part string. |
Return value
string|floatBy default a string in the form "msec sec" (fractional seconds first, then whole seconds since the epoch); a float of seconds since the epoch when as_float is true.
See also
microtime() reads the wall clock with microsecond resolution. Almost every modern use passes true to get a float, then subtracts two readings to measure how long something took.
When to reach for it
Use it to time a block of code, to build a request identifier that needs sub-second uniqueness, or to stamp a log line more precisely than time() allows. For pure duration measurement, hrtime(true) is the better tool: it reads a monotonic clock, so an NTP correction or a daylight-saving jump in the middle of your measurement cannot produce a negative or wildly inflated result.
What trips people up
The default string form is the classic trap. microtime() with no argument returns something like "0.85387600 1751020800" — the fraction comes first. Using that value in arithmetic makes PHP read only the leading 0.85387600, so your "elapsed time" is a meaningless fraction of a second. Always call microtime(true).
The float form carries roughly microsecond precision today, but a double has about 15-16 significant digits and the epoch seconds already consume ten of them, so do not expect nanosecond accuracy. And because it is a wall clock, two consecutive calls can go backwards if the system time is adjusted.
Example
When to use it
- Time a database query or a loop to compare two implementations
- Add sub-second precision to log entries so events within the same second stay ordered
- Build a collision-resistant temporary filename or request ID
Frequently asked questions
What is the difference between microtime and hrtime?
microtime reads the wall clock, so it can jump when the system time is adjusted, while hrtime reads a monotonic counter that only moves forward. Use hrtime for measuring durations and microtime when you need an actual point in time.
How do I measure how long a function takes in PHP?
Record microtime(true) before and after, then subtract: $elapsed = microtime(true) - $start. The result is in seconds as a float.
Why does microtime return two numbers separated by a space?
That is the legacy default format, "microseconds seconds", kept for backward compatibility. Pass true to get a single float instead.
More examples
Why the bare string breaks arithmetic
Casting the two-part string to a float yields just the fractional part, so the comparison prints bool(true). Splitting on the space shows the epoch seconds are the second field, not the first.
<?php
$raw = microtime(); // "0.12345600 1751020800"
echo "raw: $raw\n";
[$frac, $sec] = explode(' ', $raw);
echo 'seconds: ', $sec, "\n";
echo 'fraction: ', $frac, "\n";
// the trap: PHP reads only the leading number
var_dump((float) $raw < 1.0);A timestamp with microseconds
Splitting the float into whole seconds and microseconds lets date() format the second part while the fraction is padded to six digits, producing log stamps like 2026-08-30 11:04:07.482913.
<?php
date_default_timezone_set('UTC');
$now = microtime(true);
$whole = (int) $now;
$micro = (int) round(($now - $whole) * 1_000_000);
echo date('Y-m-d H:i:s', $whole), '.', sprintf('%06d', $micro), "\n";
echo 'plain time(): ', date('Y-m-d H:i:s', time()), "\n";
Discussion