在PHP中将数组中的连续日期分组在一起

时间:2012-10-19 04:09:46

标签: php arrays

我在数组中有以下日期(日期并不总是这些日期)

[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

我想将连续日期组合在一起,因此输出为:

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

我将如何在PHP中执行此操作?

谢谢!

2 个答案:

答案 0 :(得分:2)

这段代码将连续日期组合在一起,并了解夏令时。

数字数组

$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')
);

代码:

$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++;
        }   
    }
}

输出:

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 :(得分: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
)