dongwei1895
dongwei1895
2014-07-10 08:55

PHP日期差异,在数组之间保存每一天

已采纳

I have an array which each element contains a startdate and enddate formated in 'dd.mm.yy'.

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

Now i need to build an array that will contain every day between date_start and date_end to validate them later.

Now my question is how can i calculate every day between those dates and save it to an array ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • douzi1991 douzi1991 7年前

    This should work:

    $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')."
    ";
    }
    

    Now you only have to make this for every array entry and save it to an array

    点赞 评论 复制链接分享
  • drt5813 drt5813 7年前

    Try this as it is working for me

    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; 
    

    }

    $days = getDates('23.07.2014', '02.08.2014');

    点赞 评论 复制链接分享
  • doulu8341 doulu8341 7年前

    IF YOU JUST WANT DATE DIFFERENCE

    <?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);
        ?>
    

    OUTPUT

    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 ALL DATE BETWEEN ARRAYS

    <?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);
    ?>
    

    OUTPUT

    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
                    )
    
            )
    
    )
    
    点赞 评论 复制链接分享
  • dongli7870 dongli7870 7年前

    Something like this?

    // 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
    }
    
    点赞 评论 复制链接分享
  • douxiajia6104 douxiajia6104 7年前

    Why don't you convert dates into timestamp, then you loop from the timestamp_start until timestamp_end , by day amount of seconds, then you convert each steps into a new date to check later.

    点赞 评论 复制链接分享

为你推荐