The general microtime(true) method, however, is probably not the most accurate option.
DateTime by default is not very good and will give you seconds.
$ php -r 'echo (new \DateTime())->format("Uu");' 1483069259.000000
microtime(true) better and you get approximate microseconds, but the accuracy of the float will do some rounding.
$ php -r 'echo microtime(true);' 1483069130.6427
gettimeofday() better and gives you the exact microsecond. You can use microseconds(false) , but processing the output is more complicated.
$ php -r 'echo implode(".", array_slice(gettimeofday(), 0, 2));' 1483070039.572630
Thus, the best available instance is \DateTime , with full precision in microseconds:
\DateTime::createFromFormat("Uu", implode(".", array_slice(gettimeofday(), 0, 2)))
Ryan Dec 30 '16 at 4:03 2016-12-30 04:03
source share