PHP 8.3.4 Released!

microtime

(PHP 4, PHP 5, PHP 7, PHP 8)

microtime返回当前 Unix 时间戳和微秒数

说明

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

microtime() 返回当前 Unix 时间戳以及微秒数。本函数仅在支持 gettimeofday() 系统调用的操作系统下可用。

对于性能测量,建议使用 hrtime()

参数

as_float

如果使用并设置为 truemicrotime() 将返回浮点数而不是字符串,具体查看下面的返回值相关描述。

返回值

默认情况下,microtime() 返回“msec sec”形式的 string,其中 sec 是自 Unix 纪元(格林威治标准时间 1970 年 1 月 1 日 0:00:00)以来的秒数,msec 是自 sec 以来经过的微秒,表示为秒数的小数部分。

如果 as_float 设置为 true,然后 microtime() 返回 float,表示自 Unix 纪元以来的当前时间,以秒为单位,精确到最接近的微秒。

示例

示例 #1 计时脚本执行时间

<?php
$time_start
= microtime(true);

// Sleep 一会
usleep(100);

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

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

示例 #2 microtime()REQUEST_TIME_FLOAT

<?php
// 随机 sleep 时间
usleep(mt_rand(100, 10000));

// 在 $_SERVER 超全局数组中 REQUEST_TIME_FLOAT 是有效的。
// 包含请求开始的时间戳,精度为微秒。
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

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

参见

  • time() - 返回当前的 Unix 时间戳
  • hrtime() - 获取系统的高精度时间

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top