PHP >= 7.1 - 有效,但有一个错误!
如果您有 PHP 7.1 或更高版本,那么应该这样做:
$date = new Datetime('2018-06-05 09:06:46.7487');
$date->modify('+1 microsecond');
echo $date->format('Y-m-d H:i:s.u');
输出:
2018-06-05 09:06:46.748701
Caution: this fails for .999999
$date = new Datetime('2018-06-05 09:06:46.999999');
$date->modify('+1 microsecond');
echo $date->format('Y-m-d H:i:s.u');
输出:
2018-06-05 09:06:46.1000000
所有 PHP 版本“破解”但没有错误!
如果您有 PHP 7.0 或更早版本,那么您可以提取微秒并以“hacky”方式自己执行数学运算:
$date = new Datetime('2018-06-05 09:06:46.7487');
// Use bcadd() to add .000001 seconds to the "microtime()" of the date
$microtime = bcadd( $date->getTimestamp().'.'.$date->format( 'u' ), '.000001', 6 );
// Reconstruct the date for consumption by __construct
$date->__construct(
date( 'Y-m-d H:i:s.', explode( '.', $microtime )[ 0 ] ).explode( '.', $microtime )[ 1 ]
);
echo $date->format('Y-m-d H:i:s.u');
输出:
2018-06-05 09:06:46.748701
如果微秒为.999999,hacky 解决方案也有效
$date = new Datetime('2018-06-05 09:06:46.999999');
// Use bcadd() to add .000001 seconds to the "microtime()" of the date
$microtime = bcadd( $date->getTimestamp().'.'.$date->format( 'u' ), '.000001', 6 );
// Reconstruct the date for consumption by __construct
$date->__construct(
date( 'Y-m-d H:i:s.', explode( '.', $microtime )[ 0 ] ).explode( '.', $microtime )[ 1 ]
);
echo $date->format('Y-m-d H:i:s.u');
输出:
2018-06-05 09:06:47.000000