jworisek Posted May 30, 2006 Share Posted May 30, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/10797-calculating-workday-deadline-dates/ Share on other sites More sharing options...
jworisek Posted May 30, 2006 Author Share Posted May 30, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10797-calculating-workday-deadline-dates/#findComment-40377 Share on other sites More sharing options...
Barand Posted May 31, 2006 Share Posted May 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/10797-calculating-workday-deadline-dates/#findComment-40471 Share on other sites More sharing options...
jworisek Posted May 31, 2006 Author Share Posted May 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10797-calculating-workday-deadline-dates/#findComment-40601 Share on other sites More sharing options...
Barand Posted May 31, 2006 Share Posted May 31, 2006 [!--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 Quote Link to comment https://forums.phpfreaks.com/topic/10797-calculating-workday-deadline-dates/#findComment-40685 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.