Menu Home

microtime

osbo.com | PHP | Functions | microtime

Return current Unix timestamp with microseconds

Syntax

microtime ([ bool $get_as_float = FALSE ] ) : mixed

Parameters

get_as_float

If used and set to TRUE, microtime() will return a float instead of a string, as described in the return values section below.

Return

By default, microtime() 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. If get_as_float is set to TRUE, then microtime() returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.

Examples

1

<?

function microtime_float()
{
    list($msec, $sec) = explode(" ", microtime());
    return ((float)$msec + (float)$sec);
}

$time_start = microtime_float();
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;

echo "Did nothing in $time seconds";

?>
Did nothing in 0.00024509429931641 seconds

2

<?

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

echo "Did nothing in $time seconds";

?>
Did nothing in 0.00017094612121582 seconds

3

<?

// REQUEST_TIME_FLOAT contains the timestamp of the start of the request with microsecond precision
usleep(100);
$time_end = microtime(true);
$time = $time_end - $_SERVER["REQUEST_TIME_FLOAT"];

echo "Did nothing in $time seconds";

?>
Did nothing in 0.00094318389892578 seconds

PHP