Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

date() - Get 'date' with milliseconds #1987

Open
vlauciani opened this issue Apr 24, 2020 · 3 comments
Open

date() - Get 'date' with milliseconds #1987

vlauciani opened this issue Apr 24, 2020 · 3 comments

Comments

@vlauciani
Copy link

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.

@nusje2000
Copy link

nusje2000 commented May 20, 2020

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.

@vlauciani
Copy link
Author

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);

@nusje2000
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants