在php中的数组中将连续日期组合在一起[重复]

问题描述 投票:0回答:2
我在数组中有以下日期(日期并非总是这些日期)

[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中如何做到这一点?
    

这组代码组连续日期在一起并了解日光节省。
php arrays date grouping contiguous
2个回答
5
投票
$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)

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

1
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.