Jump to content

Recommended Posts

I'm looking into a way to computerize deadlines for our orders. At the moment a user manually enters the date by checking a cheat sheet and using a calendar to figure out when the deadline is. My problem is that I only want to use weekdays in the calculation.

For instance we might get an order we can ship in 3 days.. if we get it on a friday, I want to be able to say we will ship on the following wednesday. Some may have up to 2 month deadlines.

At the moment all I can think of is doing a rough calculation including weekends then going back through and taking out the weekends and adding that time back on... Although that will get tricky as well since I will have to loop through that possibly 3 times.

Anybody have any ideas on an easier way?
Link to comment
https://forums.phpfreaks.com/topic/10797-calculating-workday-deadline-dates/
Share on other sites

well I have something like:

[code]
<?
$today=mktime();  //todays date in seconds
$one_day=24*60*60;  //one day in seconds
$target_days=5;  //5 work days
$days_used=0;

while ($days_used<$target_days){
  $day=date("w",$today);  
  if (($day==0)||($day==6)){
    $today=$today+$one_day;
  }
  else{
    $today=$today+$one_day;
  $days_used++;
  }
}
?>
[/code]

It will work, but I'm sure there is a more efficient way.
Instead of looping through days on each of the 3 calls

[code]  // create array of weekdays
  // only loop once instead of 3 times
$weekdays = array();
for ($d=0; $d<70; $d++) {
    $day = strtotime("+$d days");
    $dow = date('w', $day);
    if ($dow != 0 && $dow != 6) {
        $weekdays[] = $day;
    }
}

$target_days=5;  //5 work days
$ship_date = date ('D d M Y', $weekdays[$target_days]);

echo $ship_date;
[/code]
In my code the most I would have is 7 iterations whereas yours will always have 70 iterations... The one I posted was different from my first thought where I might have to go back through and take out the weekends afterwords. Instead I figured it was easiest just to step through one day at a time and ignore weekends as I go. I could be wrong but that seems a lot more efficient than always pulling the max amount of days to check.
[!--quoteo(post=378508:date=May 30 2006, 09:30 PM:name=jworisek)--][div class=\'quotetop\']QUOTE(jworisek @ May 30 2006, 09:30 PM) [snapback]378508[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Some may have up to 2 month deadlines.

........ since I will have to loop through that possibly 3 times.
[/quote]

In which case 180+ iterations
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.