Reputation: 37
'from' => $this->faker->dateTime($max = 'now', $timezone = null),
'to' => $this->faker->dateTime($max = 'now', $timezone = null),
In my factory file, I use method dateTime for "from" and "to", but it create with format : yyyy-mm-dd. How can I create value for "from" with format dd-mm-yyyy minute hour second ?
Upvotes: 2
Views: 7731
Reputation: 151
I understand that Carbon is included natively in Laravel, however, if you wanted a native PHP solution, since the Faker dateTime function returns a DateTime object, you can use the format()
function of the DateTime class, the documentation for which can be found here
The code for that might look like this:
'from' => $this->faker->dateTime()->format('d-m-Y H:i:s'),
Upvotes: 2
Reputation: 7661
How about the following:
'from' => \Carbon\Carbon::parse($this->faker->dateTime($max = 'now', $timezone = null))->format('d-m-Y H:i:s'),
You can replace -
in the format()
with /
if needed. Also take a look at the Carbon docs to see what other methods are available.
Upvotes: 5