setTimezone($zone)->format('Y-m-d')); } public function addMonths(int $months): self { $index = $this->year * 12 + ($this->month - 1) + $months; $year = intdiv($index, 12); $month = $index % 12 + 1; $last = (int) (new \DateTimeImmutable(sprintf('%04d-%02d-01', $year, $month)))->format('t'); return new self($year, $month, min($this->day, $last)); } public function compare(self $other): int { return [$this->year, $this->month, $this->day] <=> [$other->year, $other->month, $other->day]; } public function isBefore(self $other): bool { return $this->compare($other) < 0; } public function isAfter(self $other): bool { return $this->compare($other) > 0; } public function equals(self $other): bool { return 0 === $this->compare($other); } public function toString(): string { return sprintf('%04d-%02d-%02d', $this->year, $this->month, $this->day); } }