$startDate ? $today : $startDate; // La période qui contient « from » peut être déjà terminée pour une date limite encore à venir : // on part de la période précédente si sa date limite n'est pas passée. $current = Calendar::periodFor( $regime, $from ); $previous = Calendar::periodFor( $regime, self::shift( $current->start, -1 ) ); if ( $previous->end >= $startDate && $previous->deadline >= $today ) { return $previous; } return $current; } /** * Rappels à envoyer aujourd'hui. * * @param string[] $alreadySent Clés des rappels déjà envoyés (« fin-de-période|jours »). * @return array */ public static function due( string $regime, string $startDate, string $today, array $alreadySent ): array { $period = self::nextDeadline( $regime, $startDate, $today ); $daysLeft = self::daysBetween( $today, $period->deadline ); $due = array(); foreach ( self::DAYS_BEFORE as $days ) { $key = $period->end . '|' . $days; // Le rappel le plus proche qui s'applique l'emporte : si le site n'a pas tourné à J-7, // on envoie celui de la veille, pas un « J-7 » en retard. if ( $daysLeft >= 0 && $daysLeft <= $days ) { if ( ! in_array( $key, $alreadySent, true ) ) { $due[] = array( 'key' => $key, 'period' => $period, 'daysLeft' => $daysLeft, ); } break; } } return $due; } public static function daysBetween( string $from, string $to ): int { $a = new \DateTimeImmutable( $from . 'T00:00:00Z' ); $b = new \DateTimeImmutable( $to . 'T00:00:00Z' ); return (int) round( ( $b->getTimestamp() - $a->getTimestamp() ) / 86400 ); } private static function shift( string $day, int $days ): string { return ( new \DateTimeImmutable( $day . 'T12:00:00Z' ) )->modify( ( $days >= 0 ? '+' : '' ) . $days . ' day' )->format( 'Y-m-d' ); } }