写给愿意动手的 PHP 开发者
PHP学习网 PHP学习网
公告PHP学习网为公益性网站,致力于为广大PHP爱好者服务
日期与时间 PHP PHP 4 microtime

microtime()

返回当前时间的微秒级表示。

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

参数

参数类型说明
$as_floatbooltrue 返回浮点数,false 返回字符串

返回值

string|float — 浮点数或「微秒 秒」格式的字符串。

示例

PHP
$t = microtime(true);
// ... 业务
printf('耗时 %.2f ms', (microtime(true) - $t) * 1000);
输出
耗时 0.03 ms

注意

  • 传 true 才能直接做减法,传 false 得到的是字符串,需要 explode。
  • 它是测量代码耗时的标准做法,比 time() 精确。

相关函数