How to group dates coming from an array?

3

I have a function here to do and I do not even know where to start:

I have an array of dates to group:

 array (size=12)<br>
      0 => string '2016-08-22' (length=10)<br>
      1 => string '2016-08-23' (length=10)<br>
      2 => string '2016-08-24' (length=10)<br>
      3 => string '2016-08-29' (length=10)<br>
      4 => string '2016-09-05' (length=10)<br>
      5 => string '2016-09-12' (length=10)<br>
      6 => string '2016-09-19' (length=10)<br>
      7 => string '2016-09-20' (length=10)<br>
      8 => string '2016-09-21' (length=10)<br>
      9 => string '2016-09-26' (length=10)<br>
      10 => string '2016-10-03' (length=10)<br>
      11 => string '2016-10-10' (length=10)<br>

I need to split this array into several arrays organized by month and year, ex:

'datas' =><br>
array(size=3)<br>
  -- 0 => array(size=4)<br>
  ------ 0 => string '2016-08-22' (length=10)<br>
  ------ 1 => string '2016-08-23' (length=10)<br>
  ------ 2 => string '2016-08-24' (length=10)<br>
  ------ 3 => string '2016-08-29' (length=10)<br>
  -- 1 => array(size=6)<br>
  ------ 0 => string '2016-09-05' (length=10)<br>
  ------ 1 => string '2016-09-12' (length=10)<br>
  ------ 2 => string '2016-09-19' (length=10)<br>
  ------ 3 => string '2016-09-20' (length=10)<br>
  ------ 4 => string '2016-09-21' (length=10)<br>
  ------ 5 => string '2016-09-26' (length=10)<br>     
  -- 2 => array(size=2)<br>
  ------ 0 => string '2016-10-03' (length=10)<br>
  ------ 1 => string '2016-10-10' (length=10)<br>

I will use these arrays to generate calendars in a function that is ready, just group the dates in arrays, which will return the calendar of each month with the dates marked.

    
asked by anonymous 14.06.2016 / 20:27

1 answer

3

You can make each month / year a key of the new array:

<?php
$datas = array(
    '2016-09-25',
    '2016-09-08',
    '2016-10-11',
    '2016-10-05',
    '2016-11-19',
    '2016-11-05',
    '2016-09-07',
);

$novasDatas = array();
foreach($datas as $data) {
    $mesAno = explode('-', $data);
    $novasDatas[$mesAno[0]. '-' .$mesAno[1]][] = $data;
}

echo '<pre>', print_r($novasDatas), '</pre>';

Result:

Array
(
    [2016-09] => Array
        (
            [0] => 2016-09-25
            [1] => 2016-09-08
            [2] => 2016-09-07
        )

    [2016-10] => Array
        (
            [0] => 2016-10-11
            [1] => 2016-10-05
        )

    [2016-11] => Array
        (
            [0] => 2016-11-19
            [1] => 2016-11-05
        )

)

If you do not want each key to be the year / month, but rather what are numeric keys do:

$datas = array(
    '2016-09-25',
    '2016-09-08',
    '2016-10-11',
    '2016-10-05',
    '2016-11-19',
    '2016-11-05',
    '2016-09-07',
);

$novasDatas = array();
$temp = array();
foreach($datas as $data) {
    $mesAno = explode('-', $data);
    $mesAno = $mesAno[0]. '-' .$mesAno[1];
    if(!in_array($mesAno, $temp)) {
        $temp[] = $mesAno;
    }
    $novasDatas[array_search($mesAno, $temp)][] = $data;
}

echo '<pre>', print_r($novasDatas), '</pre>';

Result:

Array
(
    [0] => Array
        (
            [0] => 2016-09-25
            [1] => 2016-09-08
            [2] => 2016-09-07
        )

    [1] => Array
        (
            [0] => 2016-10-11
            [1] => 2016-10-05
        )

    [2] => Array
        (
            [0] => 2016-11-19
            [1] => 2016-11-05
        )

)
    
14.06.2016 / 20:35