【问题标题】:Recursive function not returning expected result PHP [duplicate]递归函数不返回预期结果PHP [重复]
【发布时间】:2016-01-22 21:20:31
【问题描述】:

我正在尝试创建一个递归函数,该函数根据一些预定义的规则计算礼物送达日期。 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 作为最终输出

【问题讨论】:

  • 问题是什么?
  • 还添加日期的预期和当前输出。
  • 问题是函数的行为不像我预期的那样,所以需要帮助来找出函数中出了什么问题。
  • 我已经提到了预期的结果。让我编辑我的问题

标签: php date recursion


【解决方案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;
}

【讨论】:

  • 谢谢它工作正常。但我找不到你在哪里做了改变。你能告诉我问题出在哪里吗?
  • 糟糕,明白了。我没有将 $tomorrow 分配给 $tomorrow = getGiftDeliveryDate(*****)。感觉很笨:(谢谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2019-10-08
  • 2017-02-07
  • 1970-01-01
  • 2018-12-05
  • 2013-02-24
  • 2017-09-07
  • 1970-01-01
  • 2016-02-01
相关资源
最近更新 更多