snomad
snomad

Reputation: 363

PHP Faker generate unique dates in range

I'm trying to generate a unique sequence of dates in a Laravel Factory. e.g.:

2019-05-04
2019-05-05
2019-05-06
...
2019-07-04

I tried this, and each date appears 0 - n times.

$faker->unique()->dateTimeBetween('-7 days', '+2 months')->format('Y-m-d'),

Upvotes: 6

Views: 13289

Answers (1)

Farshad
Farshad

Reputation: 2000

Why don't you try this most random date time like below :

        'created_at' => $faker->dateTimeThisMonth(),

and for some time as documentation says you must do like below :

$this->faker->unique()->dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null) // DateTime('2003-03-15 02:00:49', 'Africa/Lagos')

here is a link to it :

https://github.com/fzaninotto/Faker#fakerproviderdatetime

so in your case it

$this->faker->unique()->dateTimeInInterval($startDate = '-7 days', $interval = '+ 5 days', $timezone = null) 

Upvotes: 16

Related Questions