PHP microtime() Function

Quick summary: The PHP microtime() function returns the current Unix timestamp with microseconds.

PHP microtime() Syntax

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

PHP microtime() Basic examples

var_dump(microtime(true));
PHP
Output:
float(1707398400.1234)

Returns high-precision timestamps.

PHP microtime() Real-world usage

$start = microtime(true);
usleep(500000);
echo microtime(true) - $start;
PHP
Output:
0.5

Measures execution time accurately.

PHP microtime() Edge cases

var_dump(microtime());
PHP
Output:
string(...) "0.12345600 1707398400"

Returns a string when called without arguments.

PHP microtime() Common mistakes

Forgetting the true parameter

Without true, microtime returns a string.

Incorrect
microtime();
Correct
microtime(true);

Pass true to get a float.

PHP microtime() Frequently Asked Questions

What does microtime() do?

Returns current Unix timestamp with microseconds.

Use case?

Measuring execution time.

Return format?

String or float.

Difference from time()?

Higher precision.

Performance?

Fast.

Common mistake?

Using string format incorrectly.

Can return float?

Yes with true flag.

Used in profiling?

Yes.

Safe?

Yes.

Precision level?

Microseconds.

Alternative?

hrtime().

Best practice?

Use float mode.

PHP microtime() Related PHP Functions