microtime

Return current Unix timestamp with microseconds

Syntax

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

Parameters

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 as a decimal fraction.

If 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 · void

<?

$return = microtime();

echo $return;

?>
0.97396200 1695920089

2 · as_float

<?

$as_float = true;

$return = microtime($as_float);

echo $return;

?>
1695920090.0455

3

<?

list($msec, $sec) = explode(" ", microtime());
$time = $sec + $msec;

echo $time;

?>
1695920090.1059

4

<?

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

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

echo "$time seconds";

?>
0.00016593933105469 seconds

5

<?

$as_float = true;

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

echo "$time seconds";

?>
0.0001521110534668 seconds

6

<?

// REQUEST_TIME_FLOAT contains the timestamp of the start of the request with microsecond precision

usleep(100);
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

echo "$time seconds";

?>
0.00034785270690918 seconds
HomeMenu