2016-10-01 3 views
1

키/값의 시작 날짜와 종료 날짜가있는 배열이 있습니다. 예.PHP의 날짜 범위를 결합하는 알고리즘

/* formate 'mm/dd/yyyy'=> 'mm/dd/yyyy'*/ 

$arr = array(
'01/01/2016'=>'01/01/2016', 
'01/02/2016'=>'01/02/2016', 
'01/03/2016'=>'01/03/2016', 


'04/10/2016'=>'04/10/2016', 
'04/11/2016'=>'04/11/2016', 
'04/12/2016'=>'04/12/2016', 


'04/25/2016'=>'04/25/2016', 

'04/30/2016'=>'04/30/2016', 
'05/01/2016'=>'05/01/2016', 
'05/02/2016'=>'05/02/2016' } 

) 

여기에서 일부 요소는 연속 된 날짜를 볼 수 있습니다. 예. 처음 세 요소는 04/01부터 04/03까지의 날짜를가집니다. 나는 그것을 하나의 요소로 원한다. 새로운 배열이 이렇게 있어야합니다>

$arr = array(
    '01/01/2016'=>'01/03/2016', 

    '04/10/2016'=>'04/12/2016', 

    '04/25/2016'=>'04/25/2016', 

    '04/30/2016'=>'05/02/2016' 
}) 

어떻게 할 수 있습니까?

감사

+0

날짜 오름차순으로 나되지 않습니다? –

+0

예, 월/일/년이 현명해야합니다. BTW 메인 어레이가 이미 올바르게 주문되었습니다. –

답변

2

는 - 그것에 약간의 시간을 보냈다 그렇게 싶었 몫!

예에서와 같이 원래 배열의 날짜 순서가 같고 키와 값이 항상 같다고 가정합니다.

다음과 같이 다음 기능을 사용할 수 있습니다

function group_dates($dates_array) { 

    // prepare results array 
    $result = array(); 

    // take the first date from the submitted array 
    $group_start = array_shift($dates_array); 
    $timestamp_previous_iteration = strtotime($group_start); 

    // iterate over remaining values 
    foreach ($dates_array as $date) { 
     // calculate current iteration's timestamp 
     $timestamp_current = strtotime($date); 

     // calculate timestamp for 1 day before the current value 
     $timestamp_yesterday = strtotime('-1 day', $timestamp_current); 

     if ($timestamp_previous_iteration != $timestamp_yesterday) { 
     // create group... 
     $result[$group_start] = date('m/d/Y', $timestamp_previous_iteration); 
     // ...and store the next group's start date 
     $group_start = $date; 
     } 

     // log timestamp for next iteration 
     $timestamp_previous_iteration = $timestamp_current; 
    } 

    // add final group 
    $result[$group_start] = date('m/d/Y', $timestamp_previous_iteration); 

    return $result; 
} 

당신은 당신의 원본 데이터 셋을 반복하고 다음과 같이 분류 날짜의 배열을 반환하는 함수를 사용할 수 있습니다

...,

$result_array = group_dates($arr); 

예제에서 배열에 함수를 제공하면 요청한대로 배열이됩니다.

날짜가 MM/DD/YYYY으로 지정되어 있으므로이 문자열은 strtotime()으로 유닉스 타임 스탬프로 올바르게 변환됩니다. 다른 날짜 형식을 사용했다면 PHP DateTime 객체를 살펴볼 수 있습니다.

참조
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/book.datetime.php

+0

좋습니다. 고맙습니다 –

1

확인 출력

<?php 

    $arr = array(
     '01/01/2016'=>'01/01/2016', 
     '01/02/2016'=>'01/02/2016', 
     '01/03/2016'=>'01/03/2016', 
     '04/10/2016'=>'04/10/2016', 
     '04/11/2016'=>'04/11/2016', 
     '04/12/2016'=>'04/12/2016', 
     '04/25/2016'=>'04/25/2016', 
     '04/30/2016'=>'04/30/2016', 
     '05/01/2016'=>'05/01/2016', 
     '05/02/2016'=>'05/02/2016' 
    ); 

    $lastDate = null; 
    $ranges = array(); 
    $currentRange = array(); 

    foreach ($arr as $date => $value) { 
     $temp = $value; 
     $value = date_create(date('m/d/Y', strtotime($value))); 

     if (null === $lastDate) { 
      $currentRange[] = $temp; 
     } else { 
      // get the Date object 
      $interval = date_diff($value, $lastDate); 

      if ($interval->days === 1) { 
       // add this date to the current range 
       $currentRange[] = $temp;  
      } else { 
       // store the old range and start a new 
       $ranges[] = $currentRange; 
       $currentRange = array($temp); 
      } 
     } 
     $lastDate = $value; 
    } 

    $ranges[] = $currentRange; 

    $datemerge = array(); 
    foreach ($ranges as $key) { 
     $count = count($key); 
     $datemerge[$key[0]] = $key[$count-1]; 
    } 

    print_r($datemerge); 

?> 

아래 코드 :

내가 거기에 대한 답변은 이미하지만 여기 내 버전을 알고
Array 
(
    [01/01/2016] => 01/03/2016 
    [04/10/2016] => 04/12/2016 
    [04/25/2016] => 04/25/2016 
    [04/30/2016] => 05/02/2016 
) 
+0

좋습니다. 고맙습니다. –