Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DanielFuerst's avatar

Random Start/End Date Seeder Problem

Hi Forum

I am trying to implement a seeder class that fills my table vacations with a couple of rows of vacations.

The Script is supposed to generate a random start date between to dates and then adds a random number of weeks to the start date to generate an end date. I have tried the following code inside a view file, and it works.

However when running it in a DatabaseSeeder class it does set the exact same end date as the start date. So the newly calculated end date is not inserted or not interpreted by the script.

Here is the code

class VacationsTableSeeder extends Seeder {

    public function run()
    {
        $faker = Faker::create();

        $members = User::whereHas(
            'roles', function ($q) {
            $q->where( 'name', '=', 'member' );
        }
        )->lists('id');

        foreach(range(1, 120) as $index)
        {


            $start_date = '2015-12-31 00:00:00';
            $end_date = '2010-01-01 00:00:00';

            $min = strtotime($start_date);
            $max = strtotime($end_date);

            // Generate random number using above bounds
            $val = rand($min, $max);
            $weeks = rand(1, 52);

            // Convert back to desired date format
            $start = new DateTime(date('Y-m-d H:i:s', $val));
            $end = $start->modify('+' . $weeks . ' weeks');

            Vacation::create([
                'user_id'       => $faker->randomElement( $members ),
                'title'         => $faker->sentence(3),
                'description'   => $faker->sentence(10),
                'start'         => $start,
                'end'           => $end
            ]);

        }

    }

}

Does anyone has an idea what the problem with my code could be? Thanks, Daniel

0 likes
3 replies
bobbybouwmann's avatar
Level 88

Why don't you use Carbon for this, much easier ;)

foreach(range(1, 120) as $index) {
    $date = Carbon::create(2015, 5, 28, 0, 0, 0);

    Vacation::create([
        'start'  => $date->format('Y-m-d H:i:s'),
        'end'  => $date->addWeeks(rand(1, 52))->format('Y-m-d H:i:s')
    ]);
}

Note: don't forget to import the Carbon class

10 likes
DanielFuerst's avatar

Thank you both for your quick replies @blackbird and @unglued

I have ended up using a modified version of your suggestion @blackbird

  public function run()
    {
        $faker = Faker::create();

        $members = User::whereHas(
            'roles', function ($q) {
            $q->where( 'name', '=', 'member' );
        }
        )->lists('id');

        foreach(range(1, 120) as $index)
        {

            $year = rand(2009, 2016);
            $month = rand(1, 12);
            $day = rand(1, 28);

            $date = Carbon::create($year,$month ,$day , 0, 0, 0);

            Vacation::create([
                'user_id'       => $faker->randomElement( $members ),
                'title'         => $faker->sentence(3),
                'description'   => $faker->sentence(10),
                'start'  => $date->format('Y-m-d H:i:s'),
                'end'  => $date->addWeeks(rand(1, 52))->format('Y-m-d H:i:s')
            ]);

        }

    }

Thanks very much.

2 likes

Please or to participate in this conversation.