Jump to content

Recommended Posts

I have this block of code that was written by someone years ago :-

 

<?php

	class BoDelivery {
		
		public function EstimatedDays($off, $standard_days, $saturday, $delay) {
            
            $today = date("N"); // Weekday - number 1-7
            $now = strtotime("now"); // Unix
            
            $off_array = explode(":", str_replace(".", ":", $off));
            
            $off_unix = mktime($off_array[0], $off_array[1], "00", date("n"), date("j"), date("Y"));

            $sending_days_from_now = 0;
            if ($now > $off_unix) {
                $sending_days_from_now++;
            }
            $sending_day = $today + $sending_days_from_now;
            switch ($sending_day) {
                case 6:
                    $sending_days_from_now++;
                    $sending_days_from_now++;
                    break;
                case 7:
                    $sending_days_from_now++;
                    break;
            }
            $sending_day = $today + $sending_days_from_now;

            // Estimated delivery time
            $delivery_days = $standard_days;
            $over_weekends = 0;

            // Add Delay
            if ($delay) {
                $delivery_days = $delivery_days + $delay;
            }
			            
            if ($sending_day == 5 && !$saturday) {
                $delivery_days++;
                $delivery_days++;
                $over_weekends++;
            }
            
            $delivery_day = $sending_day + $delivery_days;

            switch ($delivery_day) {
                case 6:
                    if (!$saturday) {
                        $delivery_days++;
                        $delivery_days++;
                        $over_weekends++;
                    }
                    break;
                case 7:
                    $delivery_days++;
                    $over_weekends++;
                    break;
            }
            
            if ($over_weekends == 0 && $delivery_days > 5) {
                $delivery_days++;
                $delivery_days++;
            }
            
            $delivery_day = $sending_day + $delivery_days;
            
            switch ($delivery_day) {
                case 13:
                    $delivery_days++;
                    $delivery_days++;
                    $over_weekends++;
                    break;
                case 14:
                    $delivery_days++;
                    $over_weekends++;
                    break;
            }
            
            $delivery_day = $sending_day + $delivery_days;
            
            $delivery_days = $delivery_day - $today;
            			
			return $delivery_days;
			
		}
		
	}
	
?>

 

Which is supposed to calculate the number of days for delivery, taking into account weekends & with a delay variable that we can set.

It isn't working as expected, may never have worked(??) or maybe down to PHP upgrades.

For example, if today (18th)I set the delay to 2 days the delivery estimate is the 21st (which is correct), if I change the delay to 3 OR 4 it becomes the 24th, 5 then becomes the 26th!

I can't get my head around the code at all, I wonder if someone could assist in what may be going on or add comments to the code snippets so I can maybe work it out?

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/311342-calculating-number-of-days/
Share on other sites

First step to understanding why something doesn't behave as it should is to know how it's supposed to behave.

1 hour ago, ohno said:

For example, if today (18th)I set the delay to 2 days the delivery estimate is the 21st (which is correct)

Why is that correct? Are you implying a delivery time of 1 day? Thus today (Tuesday the 18th) + 2 days of a delay before sending + 1 day of delivery = 3 days from now (Friday the 21st)?

What is $off? Is $standard_days the number of business days for delivery? $saturday is apparently whether final delivery can happen on a Saturday but does that also allow mean Saturdays count towards delivery progress? Is $delay in fact some sort of pre-shipment time or does it mean something else?

In other words, how is this function being used?

Thanks for replying. Of what I can gather, delivery is always 1 day, $off is the cutoff time for delivery that day, eg, after 4pm that delivery will not ship that day, it will ship the next business day. $standard_days is the number of business days for delivery. $saturday is whether delivery can happen on a Saturday (so if a user orders on a Friday & selects Saturday delivery).  But Saturdays should NOT count towards delivery progress for other delivery options. $delay adds day(s) as required to the delivery progress. This is how the function is currently being used :-

 

// Est Delivery
                    $today = date("N"); // Weekday - number 1-7
                    $now = strtotime("now"); // Unix

                    if ($this->mCart['deliveryoption'] == '0') {
                        $standard_days = 1;
                        $saturday_del = 0;
                        $off = $this->mDoSettings->GetSetting(175);
                        $delay = $this->mDoSettings->GetSetting(47);
                    } else {
                        $delivery = $this->mDoCart->GetPremiumDeliveryInfo($this->mCart['deliveryoption']);
                        $standard_days = $delivery['standarddays'];
                        $saturday_del = $delivery['saturdaydel'];
                        switch ($today) {
                            case 1: $off = $delivery['monoff']; break;
                            case 2: $off = $delivery['tueoff']; break;
                            case 3: $off = $delivery['wedoff']; break;
                            case 4: $off = $delivery['thuroff']; break;
                            case 5: $off = $delivery['frioff']; break;
                            case 6: $off = $delivery['satoff']; break;
                            case 7: $off = $delivery['sunoff']; break;
                        }
                        $delay = 0;
                    }
                    
                    $delivery_days = $this->mBoDelivery->EstimatedDays($off, $standard_days, $saturday_del, $delay);
                    $this->mCart['est_delivery'] = date("D jS F", strtotime("+" . $delivery_days . " days"));

 I found this script which seems to simplify things a lot but I'm not sure how to code in delivery cut off time for the day or Saturday delivery.

<?php

$holidayDates = array(
    '2020-08-19',
    
);

$count5WD = 0;
$temp = strtotime("now"); // Todays date
while($count5WD<5){
    $next1WD = strtotime('+1 weekday', $temp);
    $next1WDDate = date('Y-m-d', $next1WD);
    if(!in_array($next1WDDate, $holidayDates)){
        $count5WD++;
    }
    $temp = $next1WD;
}

$next5WD = date("D jS F", $temp);

echo $next5WD; //Displays next 5 working days

?>

 Basically, am I better off getting someone to re-code the original script or use the one I found as base for a new one?

I thought I’d sorted this but alas, no 

<?php 
class BoDelivery { public function EstimatedDaysWithHoliday($off, $days, $saturday) { 
require_once FILE_ROOT . '/data_objects/do_settings.php';
$doSettings = new DoSettings(); 
  // Holidays that happen every year in MM-DD format. 
  $holidays_every_year=($doSettings->GetSetting(51)); 
  $date_array= explode(',',$holidays_every_year); 
  $holidays_every_year= array(); 
  foreach ($date_array as $value) { 
    trim($value); 
    if (strtotime($value)=== false ){ 
      //bad date 
    }else{ 
      $holidays_every_year[] = date('m-d',strtotime($value)); 
    } 
  }; 
  // Holidays that happen only ONCE in YYYY-MM-DD format. 
  $holidays_by_year=($doSettings->GetSetting(50)); 
  $date_array= explode(',',$holidays_by_year); 
  $holidays_by_year= array(); 
  foreach ($date_array as $value) {
    trim($value); 
    if (strtotime($value)=== false )
    { 
      //bad date 
    }else{ 
      $holidays_by_year[] = date('Y-m-d',strtotime($value));
    } 
  }; 
  $today = date("N"); 
  // Weekday - number 1-7 
  $now = strtotime("now"); 
  // Unix 
  $off_array = explode(":", str_replace(".", ":", $off)); 
  $off_unix = mktime($off_array[0], $off_array[1], "00", date("n"), date("j"), date("Y")); 
  $sending_days_from_now = 0; 
  if ($now > $off_unix) { 
    $sending_days_from_now++; 
  } 
  $sending_day = $today + $sending_days_from_now; 
  switch ($sending_day) {
    case 6: $sending_days_from_now++; 
      $sending_days_from_now++; 
      break; 
    case 7: $sending_days_from_now++;
      break; 
  } $count5WD = 0; $temp = strtotime("now"); 
  // Todays date 
  if($sending_days_from_now > 0) {
    $temp = strtotime('+'.$sending_days_from_now.' weekday', $temp); 
  } 
  while($count5WD<$days){ 
    $currentDay = date('N', $temp); 
    if($currentDay == 5 && $saturday) {
      $next1WD = strtotime('+1 day', $temp); 
    } else { 
      $next1WD = strtotime('+1 weekday', $temp); 
    } $MMDD=date("m-d", $next1WD); 
    $YYYYMMDD=date("Y-m-d", $next1WD); 
    if(!(in_array($MMDD, $holidays_every_year)) && !(in_array($YYYYMMDD, $holidays_by_year))) { 
      $count5WD++; 
    } 
    $temp = $next1WD; 
  }; 
  $next5WD = date("D jS F", $temp); 
  return $next5WD; // + working days, extra day if holiday day is in array 
} 
} 
?>

With the above code it tells me standard delivery will be on Wednesday 26th it should be Tuesday 25th.

It all looks correct to me, date is N so a Saturday (today) is 6 & Sunday is 7, the code says add 2 days if a Saturday & 1 if a Sunday but it’s not doing that.

Any ideas?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.