Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faker having end date greater than start date

We are using Faker for our Laravel app. For a table we have 2 columns, start date and end date. How to call faker in a way that the end date will always be greater than start date

like image 962
Kannan Ramamoorthy Avatar asked Sep 10 '25 12:09

Kannan Ramamoorthy


1 Answers

Create a random start date in the space of time you wish:

$start = $faker->dateTimeBetween('next Monday', 'next Monday +7 days');

Then use the returned start date as the minimum input for the end date and make sure that the upper boundary for your end date is also after your start date.

$end = $faker->dateTimeBetween($start, $start->format('Y-m-d H:i:s').' +2 days');

You may also consider using Alice for creating fixtures using nice and readable YAML. You can use variables in Alice code to achieve your goal.

For the relative date/time syntax checkout the PHP docs.

like image 191
Philipp Rieber Avatar answered Sep 13 '25 04:09

Philipp Rieber