Search code examples
phplaraveldatecalendargenerate

How can I generate an array with a list of start and end dates which loop every 27 days?


Example, I'm looking to generate dates like this

    $data = [
        ['title'=>'Winter', 'start_date'=>'2018-09-20', 'end_date'=>'2018-09-27'],
        ['title'=>'Spring', 'start_date'=>'2018-09-27', 'end_date'=>'2018-10-04'],
        ['title'=>'Summer', 'start_date'=>'2018-10-04', 'end_date'=>'2018-10-11'],
        ['title'=>'Autumn', 'start_date'=>'2018-10-11', 'end_date'=>'2018-10-17'],
        ['title'=>'Winter', 'start_date'=>'2018-10-17', 'end_date'=>'2018-10-24'],
    ];

And they would loop around, the start_date of each season is 27 days apart from eachover, and each season always loops around in the correct order e.g.

Winter

Spring

Summer

Autumn

This happens every 7 days,

What I want to do is create an array which would prefill these dates 100 times, I then will call the CRON in my application every X number of days to generate more days.

I'm just confused how I would go about generating an array with these dates, I'm not sure how to start?


Solution

  • You could use something like this:

    $seasons = ['Winter', 'Spring', 'Summer', 'Autumn'];
    $startDate = \Carbon\Carbon::parse('2018-09-20');
    $data = [];
    
    for ($i=0; $i<100; ++ $i) {
        $data[] = [
            'title' => $seasons[$i % 4],
            'start_date' => $startDate->toDateString(),
            'end_date' => $startDate->addDays($i % 4 == 3 ? 6 : 7)->toDateString(),
        ];
    }
    
    dd($data);
    

    We use here % to loop over seasons, and for all cases we add 7 days except when there is Autumn (then we add 6 days to get 27 days in total instead of 28).

    As a result you will get:

    array:100 [▼
      0 => array:3 [▼
        "title" => "Winter"
        "start_date" => "2018-09-20"
        "end_date" => "2018-09-27"
      ]
      1 => array:3 [▼
        "title" => "Spring"
        "start_date" => "2018-09-27"
        "end_date" => "2018-10-04"
      ]
      2 => array:3 [▼
        "title" => "Summer"
        "start_date" => "2018-10-04"
        "end_date" => "2018-10-11"
      ]
      3 => array:3 [▼
        "title" => "Autumn"
        "start_date" => "2018-10-11"
        "end_date" => "2018-10-17"
      ]
      4 => array:3 [▼
        "title" => "Winter"
        "start_date" => "2018-10-17"
        "end_date" => "2018-10-24"
      ]
      5 => array:3 [▼
        "title" => "Spring"
        "start_date" => "2018-10-24"
        "end_date" => "2018-10-31"
      ]
      6 => array:3 [▼
        "title" => "Summer"
        "start_date" => "2018-10-31"
        "end_date" => "2018-11-07"
      ]
      7 => array:3 [▼
        "title" => "Autumn"
        "start_date" => "2018-11-07"
        "end_date" => "2018-11-13"
      ]
      8 => array:3 [▼
        "title" => "Winter"
        "start_date" => "2018-11-13"
        "end_date" => "2018-11-20"
      ]
      9 => array:3 [▶]
      10 => array:3 [▶]
      ...