setTime(0, 0); return match ($this) { self::Day => $from->modify("+{$count} days"), self::Week => $from->modify('+'.(7 * $count).' days'), self::Month => self::addMonths($from, $count), self::Year => self::addMonths($from, 12 * $count), }; } /** * The interval's length in days from the day, for the advance warning. */ public function days(DateTimeImmutable $from, int $count): int { return (int) $from->setTime(0, 0)->diff($this->next($from, $count))->days; } private static function addMonths(DateTimeImmutable $from, int $months): DateTimeImmutable { $first = $from->modify('first day of this month')->modify("+{$months} months"); $day = min((int) $from->format('j'), (int) $first->format('t')); return $first->setDate((int) $first->format('Y'), (int) $first->format('n'), $day); } }