Khalid
Khalid

Reputation: 467

How to skip the dates of an array and increment date by +1?

I have written a few lines code below using PHP, Im trying to pass a date in for loop by getting No of days and incrementing the date by the no of day, for example : i have 09-12-2015 and No days is 3 then i will get output as 09-12-2015,10-12-2015,11-12-2015. Next im using other condition where i need to increment by date if already date is present in $holidy_dates array.

$need_leave_date = "09-12-2015";     
$no_day = 6;
$holidy_dates = array('2015-12-11','2015-12-13');

$dates=array();  
for($i = 1; $i<$no_day; $i++)
{

    $leave_dates     =  date('Y-m-d', strtotime("+1 day", strtotime($leave_dates)));

    if($holidates_dates[$i] == $leave_dates) {
            $leave_dates     =  date('Y-m-d', strtotime("+1 day", strtotime($leave_dates)));
          //array_push($dates,$leave_dates);


    }
    else {       
        array_push($dates,$leave_dates);
        //  continue;
    }       


}


   //IMPLODE ALL THE DATES
   $implode_dates = implode(',', $dates); 
   echo $implode_dates;

Example I need:

  09-12-2015,10-12-2015,12-12-2015,14-12-2015,15-12-2015,16-12-2015

here available dates in $holidy_dates array are skipped.

But for me its like this:

  2015-12-10,2015-12-11,2015-12-12,2015-12-13,2015-12-14

Upvotes: 0

Views: 150

Answers (2)

Vegeta
Vegeta

Reputation: 1317

Here it will work:

$need_leave_date = "09-12-2015";     
$no_day = 6;
$holidy_dates = array('2015-12-11','2015-12-13');
$dates=array();  
$i = $no_day;
$date = date( 'Y-m-d', strtotime($need_leave_date) );
while ( $i != 0 )
{
    if( !in_array( $date, $holidy_dates  ) ) {
        array_push($dates,$date);
        $i--;
    }
    $date = date( 'Y-m-d', strtotime($date . '+1 day') );
}

$implode_dates = implode(',', $dates);

echo $implode_dates;

For multi-dimensional array:

$need_leave_date = "09-12-2015";     
$no_day = 6;
$holidy_dates = array(
                array( 'gh_date' => "2015-12-11"), 
                array( 'gh_date'=> "2015-12-13")
            );
$dates=array();  
$i = $no_day;
$date = date( 'Y-m-d', strtotime($need_leave_date) );
while ( $i != 0 )
{
    if( !in_array_r( $date, $holidy_dates  ) ) {
        array_push($dates,$date);
        $i--;
    }
    $date = date( 'Y-m-d', strtotime($date . '+1 day') );
}

$implode_dates = implode(',', $dates);

echo $implode_dates;

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

Upvotes: 1

Amit Rajput
Amit Rajput

Reputation: 2059

Here is the solution. Try this.

<?php
    $need_leave_date = "09-12-2015";     
    $no_day = 6;
    $holidy_dates = array('2015-12-11','2015-12-13');

    $dates=array();  
    $i=1;
    while($i<=$no_day){
        if($leave_dates){
            $leave_dates = date('Y-m-d', strtotime("+1 day", strtotime($leave_dates)));
        }else{
            $leave_dates = date('Y-m-d', strtotime($need_leave_date));
        }

        if(in_array($leave_dates, $holidy_dates)){
            // do nothing
        }else{
            array_push($dates,$leave_dates);
            $i++;
        }
    }
    //IMPLODE ALL THE DATES
    $implode_dates = implode(',', $dates); 
    echo $implode_dates;
?>

Output

2015-12-09,2015-12-10,2015-12-12,2015-12-14,2015-12-15,2015-12-16

Upvotes: 1

Related Questions