Laravel, Faker - Increment generated dateTime

Томица Кораћ picture Томица Кораћ · Nov 2, 2014 · Viewed 7.3k times · Source

I'm using Faker package in my Seeder to generate fake data for training events.

Each event has starts_at and ends_at fields. I want to populate the ends_at field with a DateTime that is after the one generated for starts_at, preferably by 1 to 8 hours, or even a fixed 1 hour difference would be fine.

Answer

zak.http picture zak.http · Mar 11, 2016

here is an easy way to define the ends_at

$starts_at = Carbon::createFromTimestamp($faker->dateTimeBetween($startDate = '+2 days', $endDate = '+1 week')->getTimeStamp()) ;

$ends_at= Carbon::createFromFormat('Y-m-d H:i:s', $starts_at)->addHours( $faker->numberBetween( 1, 8 ) );