lau*_*ent 466
在简短的回答是:
$milliseconds = round(microtime(true) * 1000);
Run Code Online (Sandbox Code Playgroud)
ken*_*ytm 84
使用microtime
.此函数返回由空格分隔的字符串.第一部分是秒的小数部分,第二部分是整数部分.传入true
以得到一个数字:
var_dump(microtime()); // string(21) "0.89115400 1283846202"
var_dump(microtime(true)); // float(1283846202.89)
Run Code Online (Sandbox Code Playgroud)
如果使用,请注意精度损失microtime(true)
.
还有gettimeofday
将微秒部分作为整数返回.
var_dump(gettimeofday());
/*
array(4) {
["sec"]=>
int(1283846202)
["usec"]=>
int(891199)
["minuteswest"]=>
int(-60)
["dsttime"]=>
int(1)
}
*/
Run Code Online (Sandbox Code Playgroud)
And*_*ore 40
正如其他人所说,您可以使用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);
}
Run Code Online (Sandbox Code Playgroud)
Pao*_*olo 40
简短回答:
仅限64位平台!
function milliseconds() {
$mt = explode(' ', microtime());
return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}
Run Code Online (Sandbox Code Playgroud)
答案很长:
如果你想的equilvalent功能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_INT_SIZE确定,最大值可以使用自PHP 4.4.0和PHP 5.0.5以来的常量PHP_INT_MAX.
如果您有64位整数,那么您可以使用以下函数:
function milliseconds() {
$mt = explode(' ', microtime());
return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}
Run Code Online (Sandbox Code Playgroud)
microtime()
返回自"纪元时间"以来的秒数,精确到微秒,两个数字用空格分隔,如...
0.90441300 1409263371
Run Code Online (Sandbox Code Playgroud)
第二个数字是小数部分之前的秒(整数).
该函数milliseconds
取整数部分乘以1000
1409263371000
Run Code Online (Sandbox Code Playgroud)
并将小数部分乘以1000
并舍入为0小数
1409263371904
Run Code Online (Sandbox Code Playgroud)
请注意,两者$mt[1]
和结果round
都被转换为int
.这是必要的,因为它们是float
s并且在没有强制转换的情况下对它们的操作将导致函数返回a float
.
最后,该功能稍微精确一些
round(microtime(true)*1000);
Run Code Online (Sandbox Code Playgroud)
比率为1:10(大约)的值比正确结果多1毫秒.这是由于浮点类型的精度有限(microtime(true)
返回浮点数).无论如何,如果你仍然喜欢较短的round(microtime(true)*1000);
我建议投射到int
结果.
即使它超出了问题的范围,值得一提的是,如果您的平台支持64位整数,那么您也可以在几微秒内获得当前时间而不会产生溢出.
如果事实2^63
(约为最大有符号整数)除以10^6 * 3600 * 24 * 365
(大约一年内的微秒)给出约.292471
.
这是返回的相同值 echo PHP_INT_MAX / (1000000*3600*24*365);
换句话说,有符号整数有空间存储超过200000年的时间跨度,以微秒为单位.
那你可能有
function microseconds() {
$mt = explode(' ', microtime());
return ((int)$mt[1]) * 1000000 + ((int)round($mt[0] * 1000000));
}
Run Code Online (Sandbox Code Playgroud)
小智 34
字符串变体的最短版本(32 位兼容):
$milliseconds = date_create()->format('Uv');
Run Code Online (Sandbox Code Playgroud)
Ali*_*xel 12
microtime(true)
在PHP 5中使用,或在PHP 4中进行以下修改:
array_sum(explode(' ', microtime()));
Run Code Online (Sandbox Code Playgroud)
编写该代码的便携方式是:
function getMicrotime()
{
if (version_compare(PHP_VERSION, '5.0.0', '<'))
{
return array_sum(explode(' ', microtime()));
}
return microtime(true);
}
Run Code Online (Sandbox Code Playgroud)
echo date('Y-m-d H:i:s.') . gettimeofday()['usec'];
输出:
2016-11-19 15:12:34.346351
小智 7
试试这个:
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");
}
Run Code Online (Sandbox Code Playgroud)
$timeparts = explode(" ",microtime());
$currenttime = bcadd(($timeparts[0]*1000),bcmul($timeparts[1],1000));
echo $currenttime;
Run Code Online (Sandbox Code Playgroud)
注意:由于microtime()的改进,该功能需要PHP5,并且还需要bc math模块(由于我们处理大量数字,因此您可以检查phpinfo中是否包含该模块)。
希望这对您有所帮助。
小智 5
$the_date_time = new DateTime($date_string);
$the_date_time_in_ms = ($the_date_time->format('U') * 1000) +
($the_date_time->format('u') / 1000);
Run Code Online (Sandbox Code Playgroud)
即使您使用的是32位PHP,它也可以正常工作:
list($msec, $sec) = explode(' ', microtime());
$time_milli = $sec.substr($msec, 2, 3); // '1491536422147'
$time_micro = $sec.substr($msec, 2, 6); // '1491536422147300'
Run Code Online (Sandbox Code Playgroud)
请注意,这不会给您整数,而是字符串。但是,在许多情况下,例如在为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
Run Code Online (Sandbox Code Playgroud)
或者,您也可以使用优质的一线服务:
$time_milli = (int) round(microtime(true) * 1000); // 1491536422147
$time_micro = (int) round(microtime(true) * 1000000); // 1491536422147300
Run Code Online (Sandbox Code Playgroud)
PHP 5.2.2 <
$d = new DateTime();
echo $d->format("Y-m-d H:i:s.u"); // u : Microseconds
Run Code Online (Sandbox Code Playgroud)
PHP 7.0.0 < 7.1
$d = new DateTime();
echo $d->format("Y-m-d H:i:s.v"); // v : Milliseconds
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
351728 次 |
最近记录: |