Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Estimated Shipping Date Function


sammedit

Recommended Posts

Hello forum,

I am looking for a fix to an estimated shipping date function that originally came from a contribution at http://addons.oscommerce.com/info/8286.

 

The code is supposed to return a future shipping date based on a user set amount of business days from today's date. It is supposed to skip over weekends and add extra processing time if the final estimated shipping date falls on the weekend which kicks it over to Monday. Unfortunately this code does not work correctly and returns the wrong dates when dealing with longer estimated shipping dates and when dealing with weekends. So I am hoping someone can help me get this fixed and come up with a code that actually works. I think that if there is a way to make the code pretend the weekends don't exist at all, that would be the best way to fix it. Currently the code adds processing days when it goes over or lands on Saturday or Sunday. The code that needs changed in this function is this:

 

$adddays = 0;
for ($i = 0; $i<$processdays; $i++) {
 $dayofweek = $startdayofweek + $i;  
 if ($dayofweek == "6") { $adddays = $adddays+1; }
 elseif ($dayofweek=="7") { $adddays = $adddays +1; }
 elseif ($dayofweek == "8") { $dayofweek = "1"; }

}

 

and this:

 

$dayofweekfinal = date("N",$finaldate);
if ($dayofweekfinal=='6') { $processdays = $processdays+2;  }
else if ($dayofweekfinal=='7') { $processdays = $processdays+1;  }

 

I would really appreciate any help replacing this code with code that actually works. I have posted the full function below. Please note that is also takes into account holidays and time of day BUT that code is not giving me any trouble. The only problems stem from the above code. Thank you.

 

sammedit

 

function get_early_shipping_date_estimate($cutofftime = MODULE_ESTIMATED_EARLY_SHIP_TIMES, $processdays = MODULE_ESTIMATED_EARLY_SHIP_DAYS) {
global $cart;
// Get the current time
$now = strtotime("now");

// Get the current year
$year = date("Y",$now);

// Get the time of day
$timeofday = date("H",$now);

// Get the day of the week (Monday, Tuesday..etc)
$startdayofweek = date("N",$now);


// If the time of day is greater than the cut off time for shipments AND it is not a weekend then add a day.

//if ($startdayofweek < "6"  && $timeofday > $cutofftime ) { $processdays = $processdays + 1; }

//  Count number of days to process
//  If the count comes across a Saturday or Sunday, add an additional day
//  If the count gets to 8, start over at 1
$adddays = 0;
for ($i = 0; $i<$processdays; $i++) {
 $dayofweek = $startdayofweek + $i;  
 if ($dayofweek == "6") { $adddays = $adddays+1; }
 elseif ($dayofweek=="7") { $adddays = $adddays +1; }
 elseif ($dayofweek == "8") { $dayofweek = "1"; }

}

$processdays = $processdays + $adddays;

/*
*   Get a "final" date based on the extra processing days so far
*/

$finaldate = strtotime("+".$processdays." days");


/*
*  Check and see if any holidays were in the mix of the final dates and add extra days for those
*/

$holiday_query = tep_db_query("select date from ".TABLE_SHIPPING_HOLIDAYS." where YEAR(date)='".$year."' order by date");
 while ($holidays = tep_db_fetch_array($holiday_query)) {
  if ($now <= strtotime($holidays['date'].' 00:00:00') && strtotime($holidays['date'].' 23:59:59') <= $finaldate) {   $processdays = $processdays+1; $finaldate = strtotime("+".$processdays." days");}

 }
 /*
 *   If the new "final" date falls on a Saturday or Sunday add extra processing days to land on a Monday
 */

$dayofweekfinal = date("N",$finaldate);


if ($dayofweekfinal=='6') { $processdays = $processdays+2;  }
else if ($dayofweekfinal=='7') { $processdays = $processdays+1;  }
$finalfinaldate = strtotime("+".$processdays." days");

/*
*   Then check the final ship date incase it may have landed on a holiday closure. 
*/
$senddate = check_early_ship_date($finalfinaldate);
return $senddate;
}
function check_early_ship_date($senddate) {

/*
*   Function determines if the estimated ship date is a holiday and if so, adds an additional day until it no longer is a holiday closure day.
*/
$holiday_query = tep_db_query("select date from shipping_holiday_closures where YEAR(date)='".date('Y', $senddate)."' order by date");
while ($holidays = tep_db_fetch_array($holiday_query)) {
 if ($holidays['date'] == date('Y-m-d', $senddate)) { 
  if (date("N",$senddate) == '5') { $days = 3; }
  elseif (date("N",$senddate) == '6') { $days = 2; }
  else { $days = 1; }

  $senddate = $senddate + ($days * 24 * 60 * 60); }

}

return $senddate;
}

Link to comment
Share on other sites

I wonder if a more generic date time frame would work..

Something like 7-10 days

You could just use

<?php echo date('F jS', time() + (7 * 24 * 60 * 60)); ?> to <?php echo date('F jS', time() + (10 * 24 * 60 * 60)); ?>

But add the processing days in there

So it would be

<?php echo date('F jS', time() + ((7+ $processdays) * 24 * 60 * 60 )); ?> to <?php echo date('F jS', time() + ((10+ $processdays) * 24 * 60 * 60)); ?>

 

Just a thought probably not the most elegant solution.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...