time()
以秒为单位 - 以毫秒为单位吗?
15 回答
简短的回答是:
$milliseconds = round(microtime(true) * 1000);
使用microtime
. 此函数返回一个由空格分隔的字符串。第一部分是秒的小数部分,第二部分是整数部分。传入true
以获取数字:
var_dump(microtime()); // string(21) "0.89115400 1283846202"
var_dump(microtime(true)); // float(1283846202.89)
如果您使用microtime(true)
.
还有gettimeofday
一个以整数形式返回微秒部分。
var_dump(gettimeofday());
/*
array(4) {
["sec"]=>
int(1283846202)
["usec"]=>
int(891199)
["minuteswest"]=>
int(-60)
["dsttime"]=>
int(1)
}
*/
简短的回答:
仅限 64 位平台!
function milliseconds() {
$mt = explode(' ', microtime());
return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}
[如果您运行的是 64 位 PHP,则常数PHP_INT_SIZE
等于8
]
长答案:
如果您首先想要一个time()
以毫秒为单位的等效函数,您必须考虑time()
返回自“纪元时间”(01/01/1970)以来经过的秒数,自“纪元时间”以来的毫秒数是一个很大的数字并且不适合 32 位整数。
PHP 中整数的大小可以是 32 位或 64 位,具体取决于平台。
来自http://php.net/manual/en/language.types.integer.php
整数的大小取决于平台,尽管通常值约为 20 亿的最大值(即 32 位有符号)。64 位平台的最大值通常约为 9E18,Windows 除外,它始终为 32 位。PHP 不支持无符号整数。自 PHP 4.4.0 和 PHP 5.0.5 起,可以使用常量 PHP_INT_SIZE 确定整数大小,使用常量 PHP_INT_MAX 确定最大值。
如果您有 64 位整数,则可以使用以下函数:
function milliseconds() {
$mt = explode(' ', microtime());
return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}
microtime()
返回自“纪元时间”以来的秒数,精确到微秒,两个数字用空格分隔,例如...
0.90441300 1409263371
第二个数字是秒(整数),而第一个是小数部分。
上述函数milliseconds()
取整数部分乘以1000
1409263371000
然后添加小数部分乘以1000
并四舍五入为小数点后0
1409263371904
请注意,两者$mt[1]
和结果round
都被转换为int
. 这是必要的,因为它们是float
s 并且在没有强制转换的情况下对它们进行的操作会导致函数返回 a float
。
最后,该函数比
round(microtime(true)*1000);
比率为 1:10(大约)的返回比正确结果多 1 毫秒。这是由于浮点类型的精度有限(microtime(true)
返回浮点数)。无论如何,如果您仍然更喜欢较短的round(microtime(true)*1000);
我建议转换为int
结果。
即使超出了问题的范围,也值得一提的是,如果您的平台支持 64 位整数,那么您还可以获得以微秒为单位的当前时间,而不会导致溢出。
如果事实2^63 - 1
(最大有符号整数)除以10^6 * 3600 * 24 * 365
(大约一年中的微秒)给出292471
.
这与您获得的价值相同
echo (int)( PHP_INT_MAX / ( 1000000 * 3600 * 24 * 365 ) );
换句话说,一个有符号的 64 位整数有空间存储超过 200,000 年的时间跨度(以微秒为单位)。
那时你可能有
function microseconds() {
$mt = explode(' ', microtime());
return ((int)$mt[1]) * 1000000 + ((int)round($mt[0] * 1000000));
}
正如其他人所说,您可以microtime()
用来获得时间戳的毫秒精度。
从您的评论来看,您似乎希望它作为一个高精度的 UNIX 时间戳。类似于DateTime.Now.Ticks
.NET 世界中的东西。
您可以使用以下函数来执行此操作:
function millitime() {
$microtime = microtime();
$comps = explode(' ', $microtime);
// Note: Using a string here to prevent loss of precision
// in case of "overflow" (PHP converts it to a double)
return sprintf('%d%03d', $comps[1], $comps[0] * 1000);
}
在 PHP 5 中使用microtime(true)
,或在 PHP 4 中进行以下修改:
array_sum(explode(' ', microtime()));
编写该代码的一种可移植方式是:
function getMicrotime()
{
if (version_compare(PHP_VERSION, '5.0.0', '<'))
{
return array_sum(explode(' ', microtime()));
}
return microtime(true);
}
echo date('Y-m-d H:i:s.') . gettimeofday()['usec'];
输出:
2016-11-19 15:12:34.346351
即使您使用的是 32 位 PHP,这也有效:
list($msec, $sec) = explode(' ', microtime());
$time_milli = $sec.substr($msec, 2, 3); // '1491536422147'
$time_micro = $sec.substr($msec, 2, 6); // '1491536422147300'
请注意,这不会给您整数,而是字符串。但是,这在许多情况下都可以正常工作,例如在为 REST 请求构建 URL 时。
如果您需要整数,则必须使用 64 位 PHP。
然后你可以重用上面的代码并转换为(int):
list($msec, $sec) = explode(' ', microtime());
// these parentheses are mandatory otherwise the precedence is wrong!
// ↓ ↓
$time_milli = (int) ($sec.substr($msec, 2, 3)); // 1491536422147
$time_micro = (int) ($sec.substr($msec, 2, 6)); // 1491536422147300
或者你可以使用好的 ol' one-liners:
$time_milli = (int) round(microtime(true) * 1000); // 1491536422147
$time_micro = (int) round(microtime(true) * 1000000); // 1491536422147300
尝试这个:
public function getTimeToMicroseconds() {
$t = microtime(true);
$micro = sprintf("%06d", ($t - floor($t)) * 1000000);
$d = new DateTime(date('Y-m-d H:i:s.' . $micro, $t));
return $d->format("Y-m-d H:i:s.u");
}
PHP 5.2.2 <
$d = new DateTime();
echo $d->format("Y-m-d H:i:s.u"); // u : Microseconds
PHP 7.0.0 < 7.1
$d = new DateTime();
echo $d->format("Y-m-d H:i:s.v"); // v : Milliseconds
$timeparts = explode(" ",microtime());
$currenttime = bcadd(($timeparts[0]*1000),bcmul($timeparts[1],1000));
echo $currenttime;
注意:由于 microtime() 的改进,此函数需要 PHP5,并且还需要 bc 数学模块(因为我们正在处理大量数字,您可以检查 phpinfo 中是否有该模块)。
希望这对您有所帮助。
$the_date_time = new DateTime($date_string);
$the_date_time_in_ms = ($the_date_time->format('U') * 1000) +
($the_date_time->format('u') / 1000);
字符串变体的最短版本(32 位兼容):
$milliseconds = date_create()->format('Uv');
如果您想查看真正的微秒,您需要将precision
设置更改php.ini
为 16。
之后,microsecond(true)
给了我1631882476.298437
.
所以我认为我需要将余数(298437
)除以1000,但实际上余数是0.298437
一秒。所以我需要将它乘以1000 才能得到正确的结果。
function get_milliseconds()
{
$timestamp = microtime(true);
return (int)(($timestamp - (int)$timestamp) * 1000);
}
这是我的实现,也应该在 32 位上工作。
function mstime(){
$mstime = explode(' ',microtime());
return $mstime[1].''.(int)($mstime[0]*1000);
}
用这个:
function get_millis(){
list($usec, $sec) = explode(' ', microtime());
return (int) ((int) $sec * 1000 + ((float) $usec * 1000));
}
再见