PHP microtime() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, microtime() returns the current Unix time with microsecond precision, as a string or a float.

By default it returns a string like '0.12345600 1700000000' (the fraction, a space, then the seconds). Pass true to get one float, which is the easiest way to time how long a piece of code takes.

Syntax

Call microtime() with the values you need. Start with this line.

PHP
$now = microtime(true);
CallWhat it does
microtime()Returns a string like "0.12345600 1700000000".
microtime(true)Returns a float like 1700000000.123456.
microtime(true) - $startReturns the seconds that passed since $start was taken.

The result changes on every call, so the examples below check the type and format instead of printing a raw time. For measuring code speed, hrtime(true) is even better because it cannot jump when the system clock changes.

Library declarations
PHP
microtime(bool $as_float = false): string|float

Parameters

ParameterDescription
as_floatOptional. If true, the time is returned as a float in seconds. If false (the default), a string in the form 'msec sec' is returned.

Return Value

Return ValueDescription
stringWhen as_float is false: a string 'msec sec', where msec is the fraction of a second and sec is the seconds since the epoch.
floatWhen as_float is true: the Unix time as a float, with the fraction after the decimal point.

PHP Examples

Example 1 — Check the String Format

The default result is a string with two parts. A pattern check proves the format without printing a changing value.

PHP
<?php
$m = microtime();

echo preg_match('/^0\.\d{8} \d+$/', $m) ? 'Format is "msec sec"' : 'Unexpected format', PHP_EOL;

How It Works

  1. microtime() returns something like '0. 12345600 1700000000'.
  2. The regular expression expects 0. , eight digits, a space, then the seconds.
  3. preg_match() returns 1 when the pattern matches.
  4. The ternary prints a stable message instead of the changing time.

Example 2 — String vs Float

The argument decides the return type.

PHP
<?php
echo gettype(microtime()), PHP_EOL;
echo gettype(microtime(true)), PHP_EOL;

[$fraction, $seconds] = explode(' ', microtime());
echo $fraction < 1 ? 'Fraction is below 1' : 'Unexpected', PHP_EOL;

How It Works

  1. Without an argument the result is a string.
  2. With true the result is a float, which gettype() reports as 'double'.
  3. explode() splits the string at the space into the fraction and the seconds.
  4. The fraction is always less than 1, because it is only the part after the decimal point.

Example 3 — Time a Piece of Code

Take a start time, do some work, then subtract to get the duration.

PHP
<?php
$start = microtime(true);

$sum = 0;
for ($i = 1; $i <= 100000; $i++) {
    $sum += $i;
}

$elapsed = microtime(true) - $start;

echo 'Sum: ', $sum, PHP_EOL;
echo $elapsed >= 0 ? 'Timer works' : 'Clock went backwards', PHP_EOL;

How It Works

  1. microtime(true) before the loop saves the start time as a float.
  2. The loop adds the numbers 1 to 100,000, which equals 5,000,050,000.
  3. A second microtime(true) minus the start gives the elapsed seconds.
  4. The elapsed time varies, so the example checks that it is not negative instead of printing it.

Frequently Asked Questions

By default a string like '0.12345600 1700000000'. With true it returns a float such as 1700000000.123456.
Save $start = microtime(true), run your code, then calculate microtime(true) - $start. The answer is in seconds.
time() returns whole seconds as an integer. microtime(true) adds the fraction of a second as a float.
hrtime() is better because it uses a monotonic clock that never goes backwards. microtime(true) is fine for quick, simple checks.
A float holds about 15 to 16 significant digits, so the microseconds at the end of a large timestamp can be rounded. Use hrtime(true) for exact nanoseconds.

Next: mktime()

mktime() builds a Unix timestamp from a date and time that you give in the default (local).

mktime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful