date() - Get 'date' with milliseconds
vlauciani opened this issue · 3 comments
Hi all
From PHP 7.0.0 the PHP date
function implement the v
format to print milliseconds:
what is the best practice to print date like 2018-01-03T22:34:55.345
with Faker
?
I try to use $faker->date($format = 'Y-m-d\TH:i:s.v', $max = 'now')
and It works but milliseconds are always set to 000
. Is this normal?
Thank you.
Currently the way the date is created is by creating a random integer between 0 and the current timestamp. This integer represents a unix time (seconds since January 1st, 1970). So this is the reason why the amount of milliseconds will always be 0. What you could try is to use the dateTime function and then set the amount of milliseconds yourself by passing a random integer between 0 and 1000:
$dateTime = $faker->dateTime();
$dateTime->setTime(
(int)$dateTime->format('H'),
(int)$dateTime->format('i'),
(int)$dateTime->format('s'),
$faker->randomNumber(6)
);
This solution probably isn't optimal and it would be nice if faker did this by default, or at least has a method/flag that can do this.
Hope this helps.
Thank you for your answer...
What do you think about this solution?
$faker->date($format = 'Y-m-d\TH:i:s', $max = 'now').'.'.$faker->randomNumber(6);
Yes, that seems to work as well. Maybe a function for this can be added like dateWithMicroseconds
or something like that or the v
format could be supported by the date
function.