4 replies to this topic

### #1 jworisek

jworisek
• Members
• 112 posts

Posted 30 May 2006 - 08:30 PM

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?

### #2 jworisek

jworisek
• Members
• 112 posts

Posted 30 May 2006 - 09:00 PM

well I have something like:

```<?
\$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++;
}
}
?>
```

It will work, but I'm sure there is a more efficient way.

### #3 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 31 May 2006 - 06:53 AM

Instead of looping through days on each of the 3 calls

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

### #4 jworisek

jworisek
• Members
• 112 posts

Posted 31 May 2006 - 03:46 PM

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.

### #5 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 31 May 2006 - 08:23 PM

[!--quoteo(post=378508:date=May 30 2006, 09:30 PM:name=jworisek)--][div class=\'quotetop\']QUOTE(jworisek @ May 30 2006, 09:30 PM) [/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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.