I want to divide the current month into weeks, like the first day until Saturday, and then Sunday the next Saturday. For example: in the month of May, So I want to break it down like
2016-05-01 to 2016-05-07 2016-05-08 to 2016-05-14 2016-05-15 to 2016-05-21 2016-05-22 to 2016-05-28 2016-05-29 to 2016-05-31
if I try the code below, I did not get the exact result.
<?php $start_date = date('Ym-d', strtotime('2016-06-01')); $end_date = date('Ym-d', strtotime('2016-06-30')); $i=1; for($date = $start_date; $date <= $end_date; $date = date('Ym-d', strtotime($date. ' + 7 days'))) { echo getWeekDates($date, $start_date, $end_date, $i); echo "\n"; $i++; } function getWeekDates($date, $start_date, $end_date, $i) { $week = date('W', strtotime($date)); $year = date('Y', strtotime($date)); $from = date("Ymd", strtotime("{$year}-W{$week}+1")); if($from < $start_date) $from = $start_date; $to = date("Ymd", strtotime("{$year}-W{$week}-7")); if($to > $end_date) $to = $end_date; echo $from." - ".$to.'<br>'; } ?>
i got how
2016-05-01 - 2016-05-01 2016-05-01 - 2016-05-08 2016-05-08 - 2016-05-15 2016-05-15 - 2016-05-22 2016-05-22 - 2016-05-29
In a for loop, checking the last set of days, conditon get failed.so it does not calculate 30.31.
How can i do this?
source share