Jump to content

Loop through days for following week


john-formby

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
https://forums.phpfreaks.com/topic/275850-loop-through-days-for-following-week/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.