递归函数没有返回预期的结果PHP

时间:2015-10-23 10:54:38

标签: php date recursion

我正在尝试创建一个递归函数,根据一些预定义的规则计算礼品交付日期。 1.礼品可在预订后一天送达 2.如果在周六或周日预订,那么礼物将在下一个工作日+ 1个处理日之后送达。 3.结果日期可能不在预定义的假期中。

我创建了以下功能,但它返回了错误的日期。

//The delivery date might not be from these dates
$holidays_selected = array('23-10-2015','24-10-2015','28-10-2015');

echo $gift_delivery_date = getGiftDeliveryDate(date('d-m-Y',strtotime('+1 Day')),$holidays_selected);
//It prints 25-10-2015 what i expect is 27-10-2015

function getGiftDeliveryDate($asuumed_date,$holidays){
        $tomorrow = '';
        if (in_array($asuumed_date,$holidays)) {
            $tomorrow = date('d-m-Y',strtotime($asuumed_date.'+1 Day'));
            getGiftDeliveryDate($tomorrow,$holidays);

        } else if (date('N',strtotime($asuumed_date)) == 6) {
            $tomorrow = date('d-m-Y',strtotime($asuumed_date.'+3 Day'));
                if(in_array($tomorrow,$holidays)){
                    $tomorrow = date('d-m-Y',strtotime($tomorrow.'+1 Day'));
                    getGiftDeliveryDate($tomorrow,$holidays);
                }
        } else if (date('N',strtotime($asuumed_date)) == 7) {
            $tomorrow = date('d-m-Y',strtotime($asuumed_date.'+2 Day'));
                if(in_array($tomorrow,$holidays)){
                    $tomorrow = date('d-m-Y',strtotime($tomorrow.'+1 Day'));
                    getGiftDeliveryDate($tomorrow,$holidays);
                }
        } else {
            $tomorrow = $asuumed_date;
        }

    return $tomorrow;
}

修改

我期望输出为27-10-2015,但它会将25-10-2015作为最终输出

1 个答案:

答案 0 :(得分:1)

你错过了函数的返回值

function getGiftDeliveryDate($asuumed_date, $holidays) {
    if (in_array($asuumed_date, $holidays)) {
        $tomorrow = date('d-m-Y', strtotime($asuumed_date . '+1 Day'));
        <b>$tomorrow =</b> getGiftDeliveryDate($tomorrow, $holidays);

    } else if (date('N', strtotime($asuumed_date)) == 6) {
        $tomorrow = date('d-m-Y', strtotime($asuumed_date . '+3 Day'));
        if (in_array($tomorrow, $holidays)) {
            $tomorrow = date('d-m-Y', strtotime($tomorrow . '+1 Day'));
            <b>$tomorrow =</b> getGiftDeliveryDate($tomorrow, $holidays);
        }
    } else if (date('N', strtotime($asuumed_date)) == 7) {
        $tomorrow = date('d-m-Y', strtotime($asuumed_date . '+2 Day'));
        if (in_array($tomorrow, $holidays)) {
            $tomorrow = date('d-m-Y', strtotime($tomorrow . '+1 Day'));
            <b>$tomorrow =</b> getGiftDeliveryDate($tomorrow, $holidays);
        }
    } else {
        $tomorrow = $asuumed_date;
    }

    return $tomorrow;
}
相关问题