简体   繁体   English

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

[英]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? 我将如何在PHP中做到这一点?

Thanks! 谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM