SkyRanger Posted September 19, 2019 Share Posted September 19, 2019 I am currently working on a date range script and wondering if anybody think of an easier way to do this, doing up to next 8 weeks? This one starts on a Sunday. I also need to do one starting on a Monday (will figure that one later) $thisweek = date("Y-m-d"); $week = date('w', strtotime($thisweek)); $date = new DateTime($thisweek); $firstWeek = $date->modify("-".$week." day")->format("M d"); $endWeek = $date->modify("+6 day")->format("M d"); echo $firstWeek." - "; echo $endWeek; $nextweek = date(("Y-m-d"), strtotime("+7 Days")); $week1 = date('w', strtotime($nextweek)); $date1 = new DateTime($nextweek); $firstWeek1 = $date1->modify("-".$week1." day")->format("M d"); $endWeek1 = $date1->modify("+6 day")->format("M d"); echo $firstWeek1." - "; echo $endWeek1; Quote Link to comment Share on other sites More sharing options...
Barand Posted September 19, 2019 Share Posted September 19, 2019 Use a DatePeriod $dt1 = new DateTime('next sunday'); $diwk = new DateInterval('P7D'); $di6 = new DateInterval('P6D'); $num_weeks = 8; $period = new DatePeriod($dt1, $diwk, $num_weeks-1); foreach ($period as $d) { echo $d->format('M d') . ' – ' ; $end = $d->add($di6)->format('M d'); echo "$end<br>"; } 1 Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted September 19, 2019 Author Share Posted September 19, 2019 Awesome Barand thank you. I also see what I can start it on any day. Again thank you. Much easier than what I was attempting to do and PS: This link in your signature. Still reading it over. Thank you Quote Link to comment 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.