Time()是以秒为单位的——有以毫秒为单位的吗?


当前回答

用这个: 函数get_millis () { List ($usec, $sec) =爆炸(' ',microtime()); 返回(int) ((int) $sec * 1000 + ((float) $usec * 1000)); }

Bye

其他回答

echo date('Y-m-d H:i:s.')。gettimeofday()(“购买”);

输出:

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

或者你也可以用一些简单的句子:

$time_milli = (int) round(microtime(true) * 1000);    // 1491536422147
$time_micro = (int) round(microtime(true) * 1000000); // 1491536422147300

简单的回答是:

$milliseconds = floor(microtime(true) * 1000);

用这个: 函数get_millis () { List ($usec, $sec) =爆炸(' ',microtime()); 返回(int) ((int) $sec * 1000 + ((float) $usec * 1000)); }

Bye

这是我的实现,应该工作在32位以及。

function mstime(){
    $mstime = explode(' ',microtime());
    return $mstime[1].''.(int)($mstime[0]*1000);
}