microtime
(PHP 4, PHP 5, PHP 7)
microtime - 以微秒为单位返回当前的Unix时间戳
描述
mixed microtime ([ bool $get_as_float = false ] )
microtime()以微秒的形式返回当前的Unix时间戳。此功能仅在支持gettimeofday()系统调用的操作系统上可用。
参数
get_as_float
如果使用并设置为TRUE
,microtime()将返回一个float而不是一个字符串,如下面的返回值部分所述。
返回值
默认情况下,microtime()以“msec sec”的形式返回一个字符串,其中sec是Unix纪元(格林威治标准时间1970年1月1日0:00:00)以来的秒数,msec是从秒开始经过的微秒数也用秒表示。
如果get_as_float
设置为TRUE
,则microtime()将返回一个浮点数,它表示自Unix时期以来的当前时间,精确到最近的微秒。
示例
Example #1 Timing script execution with microtime()
<?php
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
// Sleep for a while
usleep(100);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
?>
Example #2 Timing script execution in PHP 5
<?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";
?>
Example #3 microtime() and REQUEST_TIME_FLOAT (as of PHP 5.4.0)
<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));
// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo "Did nothing in $time seconds\n";
?>
另请参阅
- time() - 返回当前的Unix时间戳
← localtime
mktime →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com