start = $start instanceof DateTime ? $start : new DateTime($start); $this->end = $end instanceof DateTime ? $end : new DateTime($end); return $this; } public function __get($name) { switch ($name) { case 'start': $date = clone $this->start; return $date; case 'end': $date = clone $this->end; return $date; } throw new Exception('Unknown property ' . get_class($this) . '->' . $name); } public static function createMonthRange(DateTime $date) { switch (self::$mode) { case self::MODE_MONTH: $range = self::getMonthRange($date); break; case self::MODE_CAL_MONTH: $range = self::getCalMonthRange($date); break; case self::MODE_CAL42: $range = self::getCal42Range($date); break; default: throw new Exception('Wrong mode "' . self::$mode . '"'); } return new self($range[0], $range[1]); } protected static function getMonthRange(DateTime $date) { $start = clone $date; $end = clone $date; $start = $start->modify('first day of this month'); $end = $end->modify('last day of this month'); return [$start, $end]; } protected static function getCalMonthRange(DateTime $date) { /** @var DateTime $start */ /** @var DateTime $end */ $range = self::getMonthRange($date); $start = $range[0]; $end = $range[1]; if (1 === self::FIRST_DAY) { $start->modify('-' . ($start->format('N') - 1) . ' day'); $end->modify('+' . (7 - $end->format('N')) . ' day'); } else if (7 === self::FIRST_DAY) { $start->modify('-' . $start->format('w') . ' day'); $end->modify('+' . (6 - $end->format('w')) . ' day'); } else { throw new \Exception('Wrong first day of week "' . self::FIRST_DAY . '"'); } return [$start, $end]; } protected static function getCal42Range(DateTime $date) { /** @var DateTime $start */ /** @var DateTime $end */ $range = self::getCalMonthRange($date); $start = $range[0]; $end = $range[1]; $end->modify('+' . (42 - $end->diff($start)->days) . ' day'); return [$start, $end]; } }