我想將當前月份拆分爲幾周,如第一天到第六個星期六,然後是星期日到第二個星期六。對於例如:對於五月份,所以我想拆分爲將當前月份拆分爲php
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
如果我嘗試下面的代碼,我沒有得到確切的結果。
<?php
$start_date = date('Y-m-d', strtotime('2016-06-01'));
$end_date = date('Y-m-d', strtotime('2016-06-30'));
$i=1;
for($date = $start_date; $date <= $end_date; $date = date('Y-m-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("Y-m-d", strtotime("{$year}-W{$week}+1"));
if($from < $start_date) $from = $start_date;
$to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));
if($to > $end_date) $to = $end_date;
echo $from." - ".$to.'<br>';
}
?>
我得到了像
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
在for循環,同時檢查最後一組天,conditon得到失敗的。所以它不計算30,31。
我該怎麼做?
可能是這個問題W--我SO-8601週年數(星期一開始) – JYoThI