DragonSayler
DragonSayler

Reputation: 37

Create a datetime by faker laravel with format dd-mm-yyyy minute hour second, for example : 26/01/2022 20:52:23

'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

Answers (2)

Jack465
Jack465

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

SuperDJ
SuperDJ

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

Related Questions