microtime
Returns the current time with microsecond precision.
Syntax
microtime(bool $as_float = false): string|floatParameters
| Parameter | Type | Description |
|---|---|---|
| $as_float | bool | Optional. `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 fastNotes
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.