3

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?

Thanks!

1

2 Answers 2

5

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)
1
  • This answer does not return the asker's desired result. Oct 9, 2023 at 21:00
1
$selectedDays = array( 2012-10-18,
 2012-10-19,
 2012-10-20,
 2012-10-23,
 2012-10-24,
 2012-10-29,
 2012-10-30)

$intervals = array();
                   $i=0;
                   $j=1;
                   $diff=86400;
                   $period = $diff;
                   $nrInterval=0;
                   $intervals[$nrInterval]['start'] = $selectedDays[$i];
                   $intervals[$nrInterval]['end'] = $selectedDays[$i];
                   while($j<count($selectedDays)){
                       if(strtotime($selectedDays[$j])-strtotime($selectedDays[$i]) == $period){
                          $intervals[$nrInterval]['end'] = $selectedDays[$j];
                          $j++;
                          $period+=$diff;
                       }
                       else{
                            $i=$j;
                            $j++;
                            $nrInterval++;
                            $intervals[$nrInterval]['start'] = $selectedDays[$i];
                            $intervals[$nrInterval]['end'] = $selectedDays[$i];
                            $period = $diff;
                        }
                   }


//print_r($intervals); will output
array(
'0'=>array('start' => '2012-10-18','end' => '2012-10-20'))
.
.
.
.etc
)

Not the answer you're looking for? Browse other questions tagged or ask your own question.