我有一个包含项目的数组。每个项目都有一个标题和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