2

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'],
            ];
        }
Bham
  • 309
  • 5
  • 21
  • What is not working? I see your $items_by_week array grouped by week number. Please let us know sample output you are looking for the sample input provided – Muhammed Imran Hussain Aug 03 '17 at 10:31
  • 1
    I solved it with the following anwser: https://stackoverflow.com/questions/13128854/php-datetime-class-change-first-day-of-the-week-to-monday#answer-13129157 – Bham Aug 03 '17 at 11:41

2 Answers2

0

Try the following code

$startDate = \DateTime::createFromFormat('Y-m-d', $item['startdate']);
$week = intval($startDate->format('W'));
$day = intval($startDate->format('N'));

$a = ($day < 6 ) ? $week-1 : $week;
Denis
  • 101
  • 1
  • How could i also take account with diferent years – Bham Aug 03 '17 at 12:05
  • It's depends on minimal number of days from start of new year you want to count as a first week. In case of min = 4 , my code will work, because of implementation of DateTime in php – Denis Aug 03 '17 at 12:32
0

Grouping be week (where week begins on saturday and ends on friday is working). This is the result, with usages off the following code:

$byWeek = array(); 
foreach ($items as $item) {
    $date = DateTime::createFromFormat('Y-m-d', $item['date']);

    $firstDayOfWeek = 6; // Saturday

    $difference = ($firstDayOfWeek - $date->format('N'));
    if ($difference > 0) { $difference -= 7; }
    $date->modify("$difference days");
    $week = $date->format('W');

    if(!isset($byWeek[$week])){
        $byWeek[$week] = [];
    }

    $byWeek[$week][] = $item;
}

// result
        Array
        (

        [31] => Array
            (
                [0] => Array
                    (
                        [title] => test 11
                        [date] => 2017-08-11
                    )

            )

        [32] => Array
            (
                [0] => Array
                    (
                        [title] => test
                        [date] => 2017-08-12
                    )

                [1] => Array
                    (
                        [title] => test 222
                        [date] => 2017-08-12
                    )

                [2] => Array
                    (
                        [title] => test 1
                        [date] => 2017-08-13
                    )

                [3] => Array
                    (
                        [title] => test 2
                        [date] => 2017-08-14
                    )

            )

But now i also want to group by day, within the week. So something like the following output is desired

Array
(
    [32] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [title] => test
                            [date] => 2017-08-12
                        )

                    [1] => Array
                        (
                            [title] => test 222
                            [date] => 2017-08-12
                        )

                )

            [1] => Array
                (
                    [title] => test 1
                    [date] => 2017-08-13
                )

        )

)
Bham
  • 309
  • 5
  • 21