Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

microtime

Returns the current time with microsecond precision.

Syntax

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

Parameters

ParameterTypeDescription
$as_floatboolOptional. `true` returns a float number of seconds.

Return value

string|floatA timestamp with a fractional part.

Examples

Timing a piece of code:

<?php
 
$start = microtime(true);
 
$sum = array_sum(range(1, 100000));
 
$seconds = microtime(true) - $start;
 
echo $sum, ' ', $seconds < 1 ? 'fast' : 'slow';

Output:

5000050000 fast

Notes

For precise measurements hrtime(true) is better: it returns nanoseconds and does not depend on system clock changes.

See also

  • time — Returns the current timestamp.

Related recipes

In the tutorial

  • Date and time — Working with dates in PHP — the date function, the DateTimeImmutable class, formats, adding to dates and the difference between them.

Thanks for reading!

Donate to the site — we work hard for you.