Jump to content

Loop through days for following week


john-formby
Go to solution Solved by Zane,

Recommended Posts

Hi,

 

I need help with looping through the dates of working days (Monday to Friday) for the following week.

 

I need to send an email once a week (probably on a Thursday or Friday) informing people of their rooms for the following week.  I need to loop through each day, starting with Monday and finishing on Friday.

 

I wrote this:

for($i=0;$i<5;$i++) {
	echo date('Y-m-j', strtotime('Monday+'.$i)).'<br />';
}

I thought this would work, but it returns the correct date for next Monday (2013-03-25) and then the other 4 days are all showing as 2013-03-24.

 

Any ideas how I can get this to work?

 

Thank you for taking the time to help.

 

Many Thanks,

 

John

Link to comment
Share on other sites

I'd simplify the logic a bit, and generate the timestamp using DateInterval and DatePeriod.

// First set up the starting day, ending day (+1 day) and the interval to use (1 day).
$start = new DateTime ('next monday', new DateTimeZone ('UTC'));
$end = new DateTime ('next monday +7 days', new DateTimeZone ('UTC'));
$interval = new DateInterval ('P1D');

// Then create the period range, including the starting day and up to the ending day.
// Doesn't include the last day, which is why we added one day extra when creating the ending timestamp.
$range = new DatePeriod ($start, $interval, $end);

// Run through the days of the next week, and build up the output string.
$output = '';
foreach ($range as $date) {
$output .= $date->format ('Y-m-j')."\n";
}
Then it's just to echo out $output wherever you want the dates to be displayed. Edited by Christian F.
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.