I've an array containing items. Each item has a title and startdate. Now i want to group these items by week. If i look at the documentation, the format->('W') weeks starts on monday. I need to change this to saturday.
$items = [
[ // friday
'title' => 'test',
'startdate' => '2016-03-18', // first group
],
[ // saturday
'title' => 'test',
'startdate' => '2016-03-19', // second group
],
[ // sunday
'title' => 'test',
'startdate' => '2016-03-20', // second group
],
[ // monday
'title' => 'test',
'startdate' => '2016-03-21', // second group
],
[ // saterday
'title' => 'test',
'startdate' => '2016-03-26', // third group
],
];
I've something like this but its not working correctly
foreach($items as $item) {
// group by week
$startDate = \DateTime::createFromFormat('Y-m-d', $item['startdate']);
$currectWeek = $startDate->format('W');
$saturday = 6;
$friday = 5;
$test = clone $startDate;
if($currectWeek !== 5){
$test->modify('last friday');
}
if($currectWeek !== 6){
$test->modify('next saturday');
}
$a = $test->format('W');
if(!isset($items_by_week[$a])){
$items_by_week[$a] = [];
}
$items_by_week[$a][] = [
'title' => $item['title'],
'date' => $item['startdate'],
];
}