W3docs

microtime()

IntroductionWhen it comes to web development, timing is everything. The PHP function microtime() is a powerful tool that developers use to measure the

Introduction

When it comes to web development, timing is everything. The PHP function microtime() is a powerful tool that developers use to measure the performance of their code. This function returns the current time in seconds since the Unix epoch with microsecond precision, making it a reliable way to measure the elapsed time between two points in your code. In this article, we will delve deeper into the intricacies of this function and show you how to use it to improve your web applications' performance.

What is microtime()?

The microtime() function returns the current time in seconds since the Unix epoch (January 1, 1970, 00:00:00 UTC) with microsecond precision. The standard time() function only gives you whole seconds, so when you need to measure something that finishes in a fraction of a second — like a database query or a loop — microtime() is what you reach for.

By default, microtime() returns a string in the format "msec sec" — a fractional part (the microseconds as a decimal) followed by a space and the whole-second Unix timestamp:

echo microtime();
// Output: "0.45678900 1698765432"
//          ^ fraction   ^ whole seconds

Reading that string is awkward, so in practice you almost always pass true to get a single floating-point number you can do arithmetic on directly:

echo microtime(true);
// Output: 1698765432.456789

Syntax

microtime(bool $get_as_float = false): string|float

Available since PHP 4.0.0. The function accepts one optional boolean parameter:

  • $get_as_float — when true, returns a float of the form seconds.microseconds. When false (the default), returns the "msec sec" string described above.

If you ever need the timestamp from the default string form, split it on the space and add the two parts:

[$micro, $sec] = explode(' ', microtime());
$timestamp = (float) $sec + (float) $micro; // same as microtime(true)

Usage

The main job of microtime() is benchmarking: record microtime(true) before a block of code, record it again afterwards, and subtract. The difference is the elapsed wall-clock time in seconds. This lets you compare two approaches, find slow spots, and verify that an optimization actually helped.

A second common use is building timestamp-based identifiers. The float value changes on every call, so you can mix it into an ID. Be aware, though, that two calls in the same microsecond return the same value, and floats only hold ~15 significant digits — so microtime() alone is not collision-proof. For real unique IDs use the dedicated uniqid() function (which already incorporates microtime), optionally with uniqid('', true) for extra entropy.

microtime() vs hrtime()

microtime() reports the system clock, which can jump backward (NTP adjustments, daylight-saving changes) and produce a negative or skewed measurement. For pure duration measurements in PHP 7.3+, prefer hrtime(): it uses a monotonic high-resolution timer that never moves backward and returns nanoseconds. Use microtime() when you need an actual point in time tied to the Unix epoch; use hrtime() when you only care about how long something took.

Examples

Measuring script execution time

The classic pattern: capture the start, run your code, capture the end, subtract.

php— editable, runs on the server

This example measures the time a block of code takes and outputs the result in seconds.

Benchmarking real work

To get a number you can actually read and compare, run the operation many times and format the result. Here we time 100,000 string concatenations and print the elapsed time in milliseconds:

<?php

$start = microtime(true);

$result = '';
for ($i = 0; $i < 100000; $i++) {
    $result .= 'x';
}

$elapsed_ms = (microtime(true) - $start) * 1000;

echo "Built a " . strlen($result) . "-char string in "
     . number_format($elapsed_ms, 2) . " ms";
// Example output: Built a 100000-char string in 4.31 ms

number_format() rounds the float to two decimals so the output stays tidy. See number_format() for the full formatting options.

A timestamp-based identifier

You can derive an ID from the float, but hash it (or use it as a seed) rather than exposing the raw value:

php— editable, runs on the server

Mixing in random_int() removes the risk of two IDs colliding when generated in the same microsecond. For most applications, the built-in uniqid() function is the simpler, purpose-built choice.

Conclusion

The microtime() function is an essential tool for measuring elapsed time in PHP. Capture it before and after a block of code, subtract, and you have a precise duration. Just remember it tracks the system clock — for pure duration measurements on PHP 7.3+, reach for hrtime(), and for whole-second timestamps use time().

Practice

Practice
What is the function of the microtime() in PHP?
What is the function of the microtime() in PHP?
Was this page helpful?