<?php
$result = [];
$start = new \DateTime('2016-11-12');
$start->modify('monday this week');
$end = new \DateTime('2017-05-06');
$end->modify('monday next week');
$p = new \DatePeriod(
$start,
new \DateInterval('P1W'),
$end
);
foreach ($p as $d) {
$y = $d->format("Y");
$m = $d->format("M");
if (!array_key_exists($y, $result['years'])){
$result['years'][$y] = ['y' => $y];
}
if (!arrray_key_exists($d->format("m"), $result['years'][$y]['months'])){
$result['years'][$y]['months'][$d->format("m")] = ['m' => $m];
}
$result['years'][$y]['months'][$m]['weeks'][] = ['f' => $d, 't' => (clone $d)->modify("sunday this week")];
//$result[$d->format("Y")][$d->format("M")][] = [$d, (clone $d)->modify("sunday this week")];
}
print_r($result);
exit;
foreach ($result as $year => $months){
echo $year . PHP_EOL;
foreach ($months as $month => $weeks){
echo "--" . $month . PHP_EOL;
foreach ($weeks as $week){
echo "----" . $week[0]->format("jM") . "-" . $week[1]->format("jM") . PHP_EOL;
}
}
}
exit;
$start = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end = new DateTime('2012-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y-m") . PHP_EOL;
$interval_1 = DateInterval::createFromDateString('1 week');
$end_1 = clone $dt;
$end_1->add($interval);
$period_1 = new DatePeriod($dt, $interval_1, $end_1);
foreach ($period_1 as $dt_1){
echo "--" . $dt_1->format("Y-m-d") . PHP_EOL;
}
}
preferences:
55.34 ms | 402 KiB | 5 Q