Jump to content

[SOLVED] Calculate future dates excluding weekends


mayfair

Recommended Posts

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  :)

Link to comment
Share on other sites

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>";
	} ?>

 

 

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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.