输入:'2016-02-01','2016-02-02','2016-02-03','2016-02-04','2016-02-05','2016-02-06','2016-02-07','2016-02-08','2016-02-09','2016-02-10','2016-02-11','2016-02-13','2016-02-14','2016-02-16'
输出:2016-02-01到2016-02-11,2016-02-13到2016-02-14,2016-02-16
代码:
1 <?php 2 $a = ['2016-02-01','2016-02-02','2016-02-03','2016-02-04','2016-02-05','2016-02-06','2016-02-07','2016-02-08','2016-02-09','2016-02-10','2016-02-11','2016-02-13','2016-02-14','2016-02-16']; 3 $c = array(); 4 $length = count($a); 5 $today = new DateTime(); 6 $oneDayInterval = new DateInterval('P1D'); 7 $temp[] = $a[0]; 8 for($i=0;$i<$length;$i++){ 9 if(DateTime::createFromFormat('Y-m-d',$a[$i])->add($oneDayInterval) == DateTime::createFromFormat('Y-m-d',$a[$i+1])){ 10 $temp[] = $a[$i+1]; 11 }else{ 12 $c[] = $temp; 13 $temp = []; 14 $temp[] = $a[$i+1]; 15 } 16 } 17 foreach($c as &$value){ 18 if(count($value) > 1){ 19 $value = current($value).'到'.end($value); 20 }else{ 21 $value = implode(',',$value); 22 } 23 } 24 unset($value); 25 $d = implode(',',$c); 26 var_dump($d);