# Loop through days for following week

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

##### Share on other sites

• Solution

try adding the word "week" in there

`echo date('Y-m-j', strtotime('Monday+'. \$i . ' week')).'<br />';`
##### Share on other sites

Wouldn't it be "day"? He wants to iterate from Monday to Friday I believe.

##### 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.
##### Share on other sites

Perfect, thank you very much to both of you.

I have changed week to day as suggested and works great.

Many Thanks,

John

##### Share on other sites

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

corrected code.. please check it will work

##### 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.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.