0

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 ?

1
  • Can you also post required output format? Commented Jul 10, 2014 at 9:02

5 Answers 5

2

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
                )

        )

)
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

You need to change format of date's before make this, maybe this code can help you: [code] $newformat = explode(23.07.2014) $strDate = "$newformat[2]-$newformat[1]-$newformat[0]" [/code]
0

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.

Comments

0

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
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.