我有一個包含項目的數組。每個項目都有一個標題和startdate。現在我想按周分組這些項目。如果我查看文檔,格式 - >('W') 星期星期一開始。我需要改變這個星期六。PHP數組與星期六星期六至星期五
$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
],
];
我有這樣的事情,但它不能正常工作
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'],
];
}
什麼不起作用?我看到您的$ items_by_week數組按週數分組。請讓我們知道您正在尋找示例輸入提供的示例輸出 –
我使用以下解決方案解決它:https://stackoverflow.com/questions/13128854/php-datetime-class-change-first-day-of-the星期一到星期一#answer-13129157 – Bham