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


当前回答

正如其他人所述,您可以使用microtime()在时间戳上获得毫秒精度。

从您的评论来看,您似乎希望它是一个高精度的UNIX时间戳。类似于。net世界中的DateTime.Now.Ticks。

你可以使用以下函数来完成:

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);
}

其他回答

简单的回答是:

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

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

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

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 

正如其他人所述,您可以使用microtime()在时间戳上获得毫秒精度。

从您的评论来看,您似乎希望它是一个高精度的UNIX时间戳。类似于。net世界中的DateTime.Now.Ticks。

你可以使用以下函数来完成:

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);
}

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

Bye