Information shows that in version 7.1, DateTime::__construct() can already support milliseconds. And specifically emphasize:
This means that naive comparisons of two newly created instances will now more likely return FALSE instead of TRUE: new DateTime() == new DateTime()
However, in my code, the millisecond part is always "000000", what is the problem:
<?php
for ($i = 0; $i < 10000; ++$i) {
$date = new DateTime();
// if ($date->format('u') != '000000') {
file_put_contents('./msec.log', "$i:\t" . $date->format('s.u') . PHP_EOL, FILE_APPEND);
// }
}
Output:
0: 51.000000
1: 51.000000
2: 51.000000
……
4999: 51.000000
5000: 51.000000
5001: 51.000000
……
9997: 51.000000
9998: 51.000000
9999: 51.000000
漂亮男人2017-05-24 11:36:12
Yes, the document says it was added in php5.2.2, but I found the same problem after testing and could only return 000000
But you can use this function: microtime()
習慣沉默2017-05-24 11:36:12
I tested it locally.
7.0, showing ‘000000’
7.1, showing real data.
So, are you sure the environment you are running the code in is 7.1?