1 回答
TA贡献1863条经验 获得超2个赞
显示微秒取决于您的 PHP 版本以及您希望如何格式化时间:
PHP < 7.1:
格式12:12:12.012342(秒.微秒)
public function udate()
{
list($microSecondsPastNow, $nowTimeInSeconds) = explode(" ", microtime());
$microSeconds = $microSecondsPastNow * 1000000;
//$microSeconds is now an int, so we need to add leading zeroes to achieve
//the desired format. E.g. '000001' when we have 1 microsecond.
$formattedMicroSeconds = str_pad($microseconds, 6, '0');
$dateFormat = preg_replace('`(?<!\\\\)u`', $formattedMicroSeconds, 'H:i:s.u')
return date($dateFormat, $nowTimeInSeconds);
}
格式12:12:12.012.342(秒.毫秒.微秒)
public function udate()
{
list($microSecondsPastNow, $nowTimeInSeconds) = explode(" ", microtime());
$microSeconds = $microSecondsPastNow * 1000000;
//$microSeconds is now an int, so we need to add leading zeroes to achieve
//the desired format. E.g. '000001' when we have 1 microsecond.
$formattedMicroSeconds = str_pad($microseconds, 6, '0');
$dateFormat = preg_replace('`(?<!\\\\)u`', $formattedMicroSeconds, 'H:i:s.u')
list($milliPart, $microPart) = str_split($microSeconds, 3);
return date($dateFormat, $nowTimeInSeconds) . ".$milliPart.$microPart";
}
PHP >= 7.1:
从 7.1 版开始,new DateTime()用实际值填充微秒。以前的版本用“000000”填充。因此,如果您使用的是最新的 PHP 版本,则大部分工作已经完成。
格式12:12:12.012342(秒.微秒)
public function udate()
{
$now = new DateTime();
return $now->format('H:i:s.u');
}
格式12:12:12.012.342(秒.毫秒.微秒)
public function udate()
{
$now = new DateTime();
$microseconds = $now->format('u');
list($milliPart, $microPart) = str_split($microseconds, 3);
return $now->format('H:i:s') . ".$milliPart.$microPart";
}
- 1 回答
- 0 关注
- 200 浏览
添加回答
举报