john-formby Posted March 19, 2013 Share Posted March 19, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/275850-loop-through-days-for-following-week/ Share on other sites More sharing options...
Solution Zane Posted March 19, 2013 Solution Share Posted March 19, 2013 try adding the word "week" in there echo date('Y-m-j', strtotime('Monday+'. $i . ' week')).'<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/275850-loop-through-days-for-following-week/#findComment-1419522 Share on other sites More sharing options...
PaulRyan Posted March 19, 2013 Share Posted March 19, 2013 Wouldn't it be "day"? He wants to iterate from Monday to Friday I believe. Quote Link to comment https://forums.phpfreaks.com/topic/275850-loop-through-days-for-following-week/#findComment-1419526 Share on other sites More sharing options...
Christian F. Posted March 19, 2013 Share Posted March 19, 2013 (edited) 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 March 19, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/275850-loop-through-days-for-following-week/#findComment-1419527 Share on other sites More sharing options...
john-formby Posted March 19, 2013 Author Share Posted March 19, 2013 Perfect, thank you very much to both of you. I have changed week to day as suggested and works great. Many Thanks, John Quote Link to comment https://forums.phpfreaks.com/topic/275850-loop-through-days-for-following-week/#findComment-1419528 Share on other sites More sharing options...
P5system Posted March 19, 2013 Share Posted March 19, 2013 for($i=0;$i<5;$i++) { echo date('Y-m-j', strtotime('Monday+'. $i . ' day')).'<br />';} corrected code.. please check it will work Quote Link to comment https://forums.phpfreaks.com/topic/275850-loop-through-days-for-following-week/#findComment-1419535 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.