如何在PHP中以ms为单位获取当前时间?

oll*_*dbg 2 php time

我想得到的是非常喜欢time(),但应该是毫秒准确:

2010-11-15 21:21:00:987
Run Code Online (Sandbox Code Playgroud)

这可能在PHP?

小智 10

function udate($format, $utimestamp = null) {
  if (is_null($utimestamp))
    $utimestamp = microtime(true);

  $timestamp = floor($utimestamp);
  $milliseconds = round(($utimestamp - $timestamp) * 1000000);

  return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}

echo udate('Y-m-d H:i:s:u'); // 2010-11-15 21:21:00:987
Run Code Online (Sandbox Code Playgroud)


Thi*_*ter 9

使用microtime并将其转换为毫秒:

$millitime = round(microtime(true) * 1000);
Run Code Online (Sandbox Code Playgroud)