PHP Function Reference

PHP microtime() Function



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

Note: This function is only available on operating systems that support the gettimeofday() system call.

Syntax

microtime(as_float)

Parameters

as_float Optional. When set to true, the function will return a float instead of a string. Default is false.

Return Value

By default, the function returns a string in the form "msec sec", where sec is the number of seconds since the Unix epoch (0:00:00 January 1,1970 GMT), and msec measures microseconds that have elapsed since sec and is also expressed in seconds.

When as_float is set to true, then the function returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.

Example: microtime() example

The example below shows the usage of microtime() function.

<?php
//displaying Unix timestamp as string
print_r(microtime());

echo "\n";

//displaying Unix timestamp as float
echo microtime(true);
?>

The output of the above code will be:

0.41624500 1632920208
1632920208.4163

Example: Timing script execution

The microtime() function can be used to time a script execution. Consider the example below:

<?php
$time_start = microtime(true);

//sleep for a while
usleep(100);

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Did nothing in $time seconds\n";
?>

The output of the above code will be:

Did nothing in 0.00019216537475586 seconds

❮ PHP Date and Time Reference