Home > database >  Get all weeks between two dates
Get all weeks between two dates

Time:09-29

There are two dates:

$startDate = "01.09.2021";
$endDate = "30.09.2021";

Can you help me how to create an array with the start and end dates of weeks? For the specified dates, the result should be as follows:

$weeks = [
            [
                "start" => "01.09.2021",
                "end"   => "05.09.2021"
            ],
            [
                "start" => "06.09.2021",
                "end"   => "12.09.2021"
            ],
            [
                "start" => "13.09.2021",
                "end"   => "19.09.2021"
            ],
            [
                "start" => "20.09.2021",
                "end"   => "26.09.2021"
            ],
            [
                "start" => "27.09.2021",
                "end"   => "30.09.2021"
            ]
        ];

I tried like this

while ($dateFrom <= $dateTo) {
            $d = $dateFrom->format('d.m.Y');

            $nextDays =  $this->numberOfDays($d, $dateEnd);

            $modifier = "next sunday";

            $dates[] = $d;
            $dateFrom->modify($modifier);

            if ($nextDays < 7) {
                $dates[] = $dateEnd;
            }
        }

but the result is not what I need(

Array
(
    [0] => 01.09.2021
    [1] => 05.09.2021
    [2] => 12.09.2021
    [3] => 19.09.2021
    [4] => 26.09.2021
    [5] => 30.09.2021
)

CodePudding user response:

$startDate = "01.09.2021";
$endDate = "30.09.2021";

// All weeks
$dates = [];
// A week
$tmp = [];
// All days
for ($i = strtotime($startDate); $i <= strtotime($endDate); $i  = 86400) {
    // Day of the week
    $day = date('w', $i);
    // If the variable $tmp is empty or it is Monday
    if ($day == 1 || empty($tmp)) {
        // Set as the start date of the week
        $tmp['start'] = date('d.m.Y', $i);
    } else {
        // Update the end date of the week every time until that day is Sunday
        $tmp['end'] = date('d.m.Y', $i);
        // Set as the end date of that week
        if ($day == 0) {
            $dates[] = $tmp;
        }
    }
}
// If the end date is not Sunday, it is the last week
if (date('w', strtotime($endDate)) != 0) {
    $dates[] = $tmp;
}

echo var_export($dates, true);

CodePudding user response:

Here a solution with DateTime

$startDate  = new \DateTime('2021-09-01');
$endDate  = new \DateTime('2021-09-30');

$weeks = [];

while ($startDate < $endDate) {
    $currentWeek = clone $startDate;

    $monday = clone $currentWeek->modify(('Sunday' === $currentWeek->format('l')) ? 'Monday last week' : 'Monday this week');
    $sunday = clone $currentWeek->modify('Sunday this week');

    if ($monday < $startDate) {
        $monday = $startDate;
    }
    if ($sunday > $endDate) {
        $sunday = $endDate;
    }

    $week = ['start' => $monday->format('d.m.Y'), 'end' => $sunday->format('d.m.Y')];
    $weeks[] = $week;

    $startDate->modify(' 7 days');
}

echo var_export($weeks, true);

Output:

array (
  0 => 
  array (
    'start' => '01.09.2021',
    'end' => '05.09.2021',
  ),
  1 => 
  array (
    'start' => '08.09.2021',
    'end' => '12.09.2021',
  ),
  2 => 
  array (
    'start' => '15.09.2021',
    'end' => '19.09.2021',
  ),
  3 => 
  array (
    'start' => '22.09.2021',
    'end' => '26.09.2021',
  ),
  4 => 
  array (
    'start' => '29.09.2021',
    'end' => '30.09.2021',
  ),
)
  •  Tags:  
  • php
  • Related