查询

microtime()函数—用法及示例

「 返回当前 Unix 时间戳和微秒数的字符串表示 」


函数名称:microtime()

函数说明:microtime()函数返回当前 Unix 时间戳和微秒数的字符串表示。

适用版本:PHP 4, PHP 5, PHP 7

语法:microtime(bool $get_as_float = false) : string|float

参数:

  • $get_as_float(可选):如果设置为 true,则返回浮点数形式的时间戳。默认为 false,返回字符串形式的时间戳。

返回值:

  • 当 $get_as_float 为 false 时,返回一个字符串,格式为 "msec sec",其中 msec 是微秒数,sec 是自 Unix 纪元(January 1 1970 00:00:00 GMT)以来的秒数。
  • 当 $get_as_float 为 true 时,返回浮点数形式的时间戳,精确到微秒。

示例1(返回字符串形式的时间戳):

$start = microtime();
// 执行一些代码
$end = microtime();
$timeTaken = $end - $start;
echo "执行时间:" . $timeTaken . " 秒";

示例2(返回浮点数形式的时间戳):

$start = microtime(true);
// 执行一些代码
$end = microtime(true);
$timeTaken = $end - $start;
echo "执行时间:" . $timeTaken . " 秒";

注意事项:

  • microtime()函数返回的时间戳可能不是绝对准确的,因为它依赖于操作系统和 PHP 的实现方式。
  • 在计算时间差时,可以将返回的时间戳相减得到执行时间,单位为秒。
补充纠错
上一个函数: mime_content_type()函数
下一个函数: mhash_keygen_s2k()函数
热门PHP函数
分享链接