2014-07-10 84 views
0

我有一個數組,其中每個元素都包含在'dd.mm.yy'中格式化的startdate和enddate。PHP日期差異,保存在陣列之間的每一天

Array 
    (
     [0] = 
      (
       [date_start] = 23.07.2014 
       [date_end] = 02.08.2014 
      ) 
    ) 

現在我需要建立一個包含DATE_START和DATE_END之間每隔一天後,以驗證他們的陣列。

現在我的問題是我如何計算這些日期之間的每一天並將其保存到數組?

+0

你也可以發佈需要的輸出格式嗎? – bhushya

回答

1

這應該工作:

$startDate = new DateTime('2014-05-01'); 
$endDate = new DateTime('2014-06-02'); 
// calculate diff 
$diff = $startDate->diff($endDate); 
$daysDiff = $diff->days; 


for ($i = 0; $i < $daysDiff; $i++) { 
    // clone so the original date will not be changed 
    $currentDate = clone $startDate; 
    $currentDate->modify('+'.$i.' days'); 
    echo $currentDate->format('Y-m-d H:i:s')."\n"; 
} 

現在你只需要在此爲每個數組項,並將其保存到一個數組

+0

您需要更改日期的格式,才能這樣做,也許這段代碼可以幫助你: [code] $ newformat = explode(23.07.2014) $ strDate =「$ newformat [2] - $ newformat [1] - $ newformat [0]「 [/ code] – fermin

+1

new DateTime('23 .07.2014');應該工作 – gries

+0

是的,我不知道DateTime採取兩個字符串格式,thx。 – fermin

0

爲什麼不將日期轉換爲時間戳,然後從timestamp_start循環到timestamp_end,以秒爲單位,然後將每個步驟轉換爲新日期以便稍後檢查。

0

像這樣的事情?

// Convert start and end dates to unix timestamp 
$start = strptime($date_array[0]['date_start'], '%d.%m.%Y'); 
$end = strptime($date_array[0]['date_end'], '%d.%m.%Y'); 

$dates = array(); 
while ($start < $end){ 
    $dates[] = date('d.m.Y', $start); 
    $start += 86400 // One day equals 86400 seconds 
} 
2

如果你只是想日期的差異

<?php 
     $arr=array 
     (
      array 
       (
        'date_start' =>'23.07.2014', 
        'date_end' =>'02.08.2014' 
       ), 
      array 
      (
       'date_start' =>'15.07.2014', 
       'date_end' =>'02.08.2014' 
      ) 
     ); 

     foreach($arr as $key=>$val){ 
      $diff=strtotime($val['date_end'])-strtotime($val['date_start']); 
      $new_arr[]=array(
           'day_start'=>$val['date_start'], 
           'day_end'=>$val['date_end'], 
           'difference'=>$diff/(24*60*60)-1 
          ); 
     } 
     print_r($new_arr); 
    ?> 

輸出

Array 
(
    [0] => Array 
     (
      [day_start] => 23.07.2014 
      [day_end] => 02.08.2014 
      [difference] => 9 
     ) 

    [1] => Array 
     (
      [day_start] => 15.07.2014 
      [day_end] => 02.08.2014 
      [difference] => 17 
     ) 

) 

IF YOU WANT陣列之間的所有日期

<?php 
    $arr=array 
    (
     array 
      (
       'date_start' =>'23.07.2014', 
       'date_end' =>'02.08.2014' 
      ), 
     array 
     (
      'date_start' =>'15.07.2014', 
      'date_end' =>'02.08.2014' 
     ) 
    ); 

    foreach($arr as $key=>$val){ 
     $diff=strtotime($val['date_end'])-strtotime($val['date_start']); 
     $day_diff=$diff/(24*60*60); 
     $alldiff=array(); 
     for($i=1;$i<=$day_diff-1;$i++){ 

      $alldiff[]=date('d.m.Y',(strtotime($val['date_start'])+$i*24*60*60)); 
     } 
      $new_arr[]=array(
           'day_start'=>$val['date_start'], 
           'day_end'=>$val['date_end'], 
           'difference'=>$alldiff 
          ); 

    } 
    echo "<pre>"; 
    print_r($new_arr); 
?> 

輸出

Array 
(
    [0] => Array 
     (
      [day_start] => 23.07.2014 
      [day_end] => 02.08.2014 
      [difference] => Array 
       (
        [0] => 24.07.2014 
        [1] => 25.07.2014 
        [2] => 26.07.2014 
        [3] => 27.07.2014 
        [4] => 28.07.2014 
        [5] => 29.07.2014 
        [6] => 30.07.2014 
        [7] => 31.07.2014 
        [8] => 01.08.2014 
       ) 

     ) 

    [1] => Array 
     (
      [day_start] => 15.07.2014 
      [day_end] => 02.08.2014 
      [difference] => Array 
       (
        [0] => 16.07.2014 
        [1] => 17.07.2014 
        [2] => 18.07.2014 
        [3] => 19.07.2014 
        [4] => 20.07.2014 
        [5] => 21.07.2014 
        [6] => 22.07.2014 
        [7] => 23.07.2014 
        [8] => 24.07.2014 
        [9] => 25.07.2014 
        [10] => 26.07.2014 
        [11] => 27.07.2014 
        [12] => 28.07.2014 
        [13] => 29.07.2014 
        [14] => 30.07.2014 
        [15] => 31.07.2014 
        [16] => 01.08.2014 
       ) 

     ) 

) 
0

試試這個,因爲它是爲我工作

function getDates($startTime, $endTime) { 
$day = 86400; 
$format = 'd.m.Y'; 
$startTime = strtotime($startTime); 
$endTime = strtotime($endTime); 
$numDays = round(($endTime - $startTime)/$day); 

$days = array(); 

for ($i = 1; $i < $numDays; $i++) { 
    $days[] = date($format, ($startTime + ($i * $day))); 
} 

return $days; 

}

$天= getDates('23 .07.2014' ,'02 .08.2014' );