mayfair Posted July 23, 2009 Share Posted July 23, 2009 Hello guys Im writing an online ordering system but have come stuck trying to calculate specific future dates. What im trying to do im sure is fairly simple, but ive searched the forum and Google for the best part of an hour now and haven't got anywhere. Im trying to populate a drop down list with a list of future dates (4 weeks worth) between 2 specified date periods that excludes weekends (and eventually bank holidays if I can get this working). Im currently using 'strtotime' to generate FROM and TO dates that are 4 weeks apart. When a user selects one date period - for example: FROM strtotime(+7 days) TO strtotime(+27 days), I would like to populate a drop down list on another page that includes all the dates that fell between +7 days in the future to +27 days in the future but excluding weekends. At the moment, users can select a delivery period from a drop down list on the first order page which passes a value 'period1', 'period2', 'period3' and so on over to the second order page. This is the page I am working on, and would like to populate a drop down list with exact dates based on which period was chosen using a bunch of IF statements (IF period1, load these dates etc.). I hope ive made myself clear, I can post the code if necessary. Any help would be greatly appreciated Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted July 23, 2009 Share Posted July 23, 2009 $from = 7; $duration = 28; for ($i = strtotime('+'.$from.' days'); $i <= strtotime('+'.($from+$duration).' days'); $i += 86400) { if (date('N',$i) < 6) { echo date('D, d-M-Y',$i).'<br />'; } } Quote Link to comment Share on other sites More sharing options...
mayfair Posted July 23, 2009 Author Share Posted July 23, 2009 Thank you Mark, that was exactly what I was looking for. I'll include the adapted working solution below incase anyone wants to know it for future reference: <?php if ($date == "period1") { echo "<select name='date'>"; $from = 7; $duration = 28; for ($i = strtotime('+'.$from.' days'); $i <= strtotime('+'.($from+$duration).' days'); $i += 86400) { if (date('N',$i) < 6) { echo "<option value='date('d/m/y',$i)'>".date('d/m/y',$i)."</option>"; } } echo "</select>"; } ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted July 23, 2009 Share Posted July 23, 2009 You should never use functions in a for loop's second expression, when it can be avoided. The code is run on every iteration, so assign what you can to a variable before the loop, for better efficiency: $end_stamp = strtotime('+' . ($from + $duration) . ' days'); for ($i = strtotime("+ $from days"); $i <= $end_stamp; $i += 86400) { Quote Link to comment 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.