$last) { throw new InvalidReportPeriod; } $this->from = $from; $this->to = $to; } public static function month(DateTimeImmutable $day): self { return new self($day->format('Y-m-01'), $day->format('Y-m-t')); } public static function previousMonth(DateTimeImmutable $day): self { return self::month($day->modify('first day of previous month')); } public static function year(DateTimeImmutable $day): self { return new self($day->format('Y-01-01'), $day->format('Y-12-31')); } public static function previousYear(DateTimeImmutable $day): self { return self::year($day->modify('first day of january last year')); } /** * The first month (Y-m), for data kept by month such as billing. */ public function fromMonth(): string { return substr($this->from, 0, 7); } public function toMonth(): string { return substr($this->to, 0, 7); } public function dayCount(): int { return (int) (new DateTimeImmutable($this->from))->diff(new DateTimeImmutable($this->to))->days + 1; } private static function parse(string $value): ?DateTimeImmutable { if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) !== 1) { return null; } $date = DateTimeImmutable::createFromFormat('!Y-m-d', $value); return $date !== false && $date->format('Y-m-d') === $value ? $date : null; } }