1

I am trying to create a recursive function which calculates gift delivery dates according to some predefined rules which are:

  1. A gift can be delivered after one day of booking
  2. If booked on Saturday or Sunday then the gift will be delivered after the next working day + one processing day.
  3. The resulting date might not be in predefined holidays.

I have created the following function, but it’s returning me incorrect date.

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

I expect 27-10-2015 as the output, but it is giving 25-10-2015 as the final output.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vidhyut Pandya
  • 1,605
  • 1
  • 14
  • 27

1 Answers1

1

You missed the return values from your function:

function getGiftDeliveryDate($asuumed_date, $holidays) {
    if (in_array($asuumed_date, $holidays)) {
        $tomorrow = date('d-m-Y', strtotime($asuumed_date . '+1 Day'));
        $tomorrow = 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;
}
tino.codes
  • 1,512
  • 1
  • 12
  • 23
  • 1
    Thanks it is working fine. But i could not find out where you did changes. Can you please tell me where the problem was? – Vidhyut Pandya Oct 23 '15 at 11:08
  • Oops got it. I was not assigning $tomorrow to `$tomorrow = getGiftDeliveryDate(*****)`. Feeling dumb :( Thanks for your help – Vidhyut Pandya Oct 23 '15 at 11:10
  • `$tomorrow =` does not render (I presume it is for presentation here and is invalid PHP. It may have worked in the past, but not currently(?)—there was a meta post about this recently, but now I can't locate it (perhaps it was deleted)). Perhaps use some other way, like standard code comments? – Peter Mortensen Jun 01 '22 at 23:17