如何跳过数组的日期并将日期增加+1?

时间:2015-12-24 04:39:35

标签: php mysql arrays date

我在下面使用PHP编写了几行代码,我试图通过获取No of days并将日期增加到no日期来传递for循环中的日期,例如:我有09-12-2015和No天是3然后我将得到输出为09-12-2015,10-12-2015,11-12-2015。接下来我使用其他条件,如果$ holidy_dates数组中已存在日期,我需要按日期递增。

UIButton* yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.layer.backgroundColor = [UIColor redColor].CGColor;

UIBarButtonItem* buttonItem = [[UIBarButtonItem alloc] initWithCustomView: yourButton];
self.navigationBar.leftBArButtonItems = @[buttonItem];

示例我需要:

$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;

此处可以跳过$ holidy_dates数组中的可用日期。

但对我来说就像这样:

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

2 个答案:

答案 0 :(得分:1)

这将起作用:

$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;

对于多维数组:

$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;
}

答案 1 :(得分:1)

这是解决方案。试试这个。

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

输出

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