Archived

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

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?

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.

Share on other sites
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]

Share on other sites
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.

Share on other sites
[!--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