略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: gettimeofday

2024-11-15

gettimeofday

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

gettimeofday取得当前时间

说明

gettimeofday(bool $return_float = false): mixed

本函数是 gettimeofday(2) 的接口。返回一个关联数组,包含有系统调用返回的数据。

参数

return_float

当其设为 true 时,会返回一个浮点数而不是一个数组。

返回值

默认返回一个 array。如果 return_float 设置了则会返回一个 float

数组中的键为:

  • "sec" - 自 Unix 纪元起的秒数
  • "usec" - 微秒数
  • "minuteswest" - 格林威治向西的分钟数
  • "dsttime" - 夏令时修正的类型

更新日志

版本 说明
5.1.0 增加了参数 return_float

范例

示例 #1 gettimeofday() 例子

<?php
print_r
(gettimeofday());

echo 
gettimeofday(true);
?>

以上例程的输出类似于:

Array
(
    [sec] => 1073504408
    [usec] => 238215
    [minuteswest] => 0
    [dsttime] => 1
)

1073504408.23910
add a noteadd a note

User Contributed Notes 2 notes

up
6
middleto at pilot dot msu dot edu
22 years ago
The types of DST correction (from sys/time.h on a Linux system):
0     Not on DST
1     USA DST
2     Austrailian DST
3     Western European DST
4     Middle European DST
5     Eastern European DST
6     Canada DST
7     Great Britain and Eire DST
8     Rumania DST
9     Turkey
10     Australian DST (with shift in 1986)
up
-3
lucas dot karisny at linuxmail dot org
17 years ago
A small improvement on getTimer.  Using vsprintf instead of sprintf there is no need to assign the array:

<?php
function utime()
{
  return (float) (
vsprintf('%d.%06d', gettimeofday()));
}
?>

In a test on my machine getTimer took 0.037519 seconds to run through 1000 iterations versus 0.027912 seconds for utime.  In total, utime runs about 25% quicker.  The use is negligible in an actual benchmarking scenario, but this could provide a slightly more accurate estimate.  Of course the time it takes to run the function could always be stored at the start and subtracted from your total value each time it is run.

官方地址:https://www.php.net/manual/en/function.gettimeofday.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3