$billed Months billed before, as Y-m. * @return list */ public static function unbilled(string $startsOn, ?string $endsOn, string $period, array $billed): array { $end = $endsOn === null ? $period : min($period, substr($endsOn, 0, 7)); $billedMonths = array_flip($billed); $months = []; for ($month = substr($startsOn, 0, 7); $month <= $end; $month = self::next($month)) { if (! isset($billedMonths[$month])) { $months[] = $month; } } return $months; } /** * @throws InvalidBillingPeriod */ public static function ensureValid(string $period): string { if (preg_match('/^(\d{4})-(0[1-9]|1[0-2])$/', $period) !== 1) { throw new InvalidBillingPeriod; } return $period; } /** * "11/2026" for "2026-11". */ public static function label(string $month): string { return ((int) substr($month, 5, 2)).'/'.substr($month, 0, 4); } private static function next(string $month): string { $year = (int) substr($month, 0, 4); $number = (int) substr($month, 5, 2) + 1; if ($number > 12) { $year++; $number = 1; } return sprintf('%04d-%02d', $year, $number); } }