format( self::FORMAT ); } /** * Returns the inclusive start of a period that ends today. * * The previous period of the same length starts at `period_start( 2 * $days )` * and ends, exclusively, at `period_start( $days )`. * * @param int $days Number of calendar days, including today. */ public static function period_start( int $days ): string { $days = \max( 1, $days ); $midnight = \current_datetime()->setTime( 0, 0 ); $start = $midnight->modify( '-' . ( $days - 1 ) . ' days' ); return ( false === $start ? $midnight : $start )->format( self::FORMAT ); } /** * Returns the exclusive end of a period that ended a number of days ago. * * Zero means the period is still open, so no upper bound applies. * * @param int $days_ago Length of the newer periods that follow this one. */ public static function period_end( int $days_ago ): string { return $days_ago > 0 ? self::period_start( $days_ago ) : self::OPEN_END; } /** * Returns the site-local time exactly a number of days ago. * * Retention uses this rolling cutoff; zero days means "now". * * @param int $days Days before now. */ public static function days_ago( int $days ): string { return self::minutes_ago( \max( 0, $days ) * 24 * 60 ); } /** * Returns the site-local time a number of minutes ago. * * @param int $minutes Minutes before now. */ public static function minutes_ago( int $minutes ): string { $now = \current_datetime(); $value = $now->modify( '-' . \max( 0, $minutes ) . ' minutes' ); return ( false === $value ? $now : $value )->format( self::FORMAT ); } }