phparraysdategroupingcontiguous

Grouping consecutive dates in an array together in PHP


I have the following dates in an array (dates will not always be these dates)

[0] 2012-10-18
[1] 2012-10-19
[2] 2012-10-20
[3] 2012-10-23
[4] 2012-10-24
[5] 2012-10-29
[6] 2012-10-30

I want to group consecutive dates together so the output is:

2012-10-18 to 2012-10-20
2012-10-23 to 2012-10-24
2012-10-29 to 2012-10-30

How would I do this in PHP?


Solution

  • This piece of code groups consecutive dates together and understands daylight saving.

    Array of numbers

    $dates = array
    (
    strtotime('2012-10-01'),
    
    strtotime('2012-10-03'),
    strtotime('2012-10-04'),
    strtotime('2012-10-05'),
    strtotime('2012-10-06'),
    strtotime('2012-10-07'),
    
    strtotime('2012-10-10'),
    strtotime('2012-10-11'),
    strtotime('2012-10-12'),
    strtotime('2012-10-13'),
    strtotime('2012-10-14'),
    strtotime('2012-10-15'),
    strtotime('2012-10-16'),
    strtotime('2012-10-17'),
    strtotime('2012-10-18'),
    strtotime('2012-10-19'),
    strtotime('2012-10-20'),
    
    strtotime('2012-10-23'),
    strtotime('2012-10-24'),
    strtotime('2012-10-25'),
    strtotime('2012-10-26'),
    strtotime('2012-10-29'),
    strtotime('2012-10-30'),
    strtotime('2012-10-31'),
    strtotime('2012-11-01'),
    strtotime('2012-11-02'),
    
    strtotime('2012-11-04')
    );
    

    Code:

    $conseq = array(); 
    $ii = 0;
    $max = count($dates);
    
    for($i = 0; $i < count($dates); $i++) {
        $conseq[$ii][] = date('Y-m-d',$dates[$i]);
    
        if($i + 1 < $max) {
            $dif = $dates[$i + 1] - $dates[$i];
            if($dif >= 90000) {
                $ii++;
            }   
        }
    }
    

    Outputs:

    array
      0 => 
        array
          0 => string '2012-10-01' (length=10)
      1 => 
        array
          0 => string '2012-10-03' (length=10)
          1 => string '2012-10-04' (length=10)
          2 => string '2012-10-05' (length=10)
          3 => string '2012-10-06' (length=10)
          4 => string '2012-10-07' (length=10)
      2 => 
        array
          0 => string '2012-10-10' (length=10)
          1 => string '2012-10-11' (length=10)
          2 => string '2012-10-12' (length=10)
          3 => string '2012-10-13' (length=10)
          4 => string '2012-10-14' (length=10)
          5 => string '2012-10-15' (length=10)
          6 => string '2012-10-16' (length=10)
          7 => string '2012-10-17' (length=10)
          8 => string '2012-10-18' (length=10)
          9 => string '2012-10-19' (length=10)
          10 => string '2012-10-20' (length=10)
      3 => 
        array
          0 => string '2012-10-23' (length=10)
          1 => string '2012-10-24' (length=10)
          2 => string '2012-10-25' (length=10)
          3 => string '2012-10-26' (length=10)
      4 => 
        array
          0 => string '2012-10-29' (length=10)
          1 => string '2012-10-30' (length=10)
          2 => string '2012-10-31' (length=10)
          3 => string '2012-11-01' (length=10)
          4 => string '2012-11-02' (length=10)
      5 => 
        array
          0 => string '2012-11-04' (length=10)