year = $year; $this->month = $month; $this->fillDays($posts); } /** * @param $posts * @throws \Exception */ protected function fillDays($posts) { $start = DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2))); $interval = new DateInterval('P1D'); $end = clone $start; $end->add(new DateInterval('P1M')); $period = new DatePeriod($start, $interval, $end); foreach ($period as $dt) { $post = null; if (count($posts) > 0 && $posts->first()->date_post->format('Ymd') === $dt->format('Ymd')) { $post = $posts->shift(); } $this->days[] = new Day($dt, $post); } } /** * @return string */ public function getDate() { return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2))) ->format('F Y'); } /** * @return Day[] */ public function getDays(): array { return $this->days; } /** * @return DateTime|false */ public function getPrevious() { return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2))) ->modify('-1 month'); } /** * @return DateTime|false */ public function getNext() { return DateTime::createFromFormat('Y-m-d', sprintf('%s-%s-01', $this->year, str_pad($this->month, '0', 2))) ->modify('+1 month'); } /** * @return string */ public function render(): string { $content = '