protected static function get_backups_to_delete($backupfiles, $now) {
        $config = get_config('backup');
        $maxkept = (int)$config->backup_auto_max_kept;
        $deletedays = (int)$config->backup_auto_delete_days;
        $minkept = (int)$config->backup_auto_min_kept;

        // Sort by keys descending (newer to older filemodified).
        krsort($backupfiles);
        $tokeep = $maxkept;
        if ($deletedays > 0) {
            $deletedayssecs = $deletedays * DAYSECS;
            $tokeep = 0;
            $backupfileskeys = array_keys($backupfiles);
            foreach ($backupfileskeys as $timemodified) {
                $mustdeletebackup = $timemodified < ($now - $deletedayssecs);
                if ($mustdeletebackup || $tokeep >= $maxkept) {
                    break;
                }
                $tokeep++;
            }

            if ($tokeep < $minkept) {
                $tokeep = $minkept;
            }
        }

        if (count($backupfiles) <= $tokeep) {
            // There are less or equal matching files than the desired number to keep, there is nothing to clean up.
            return false;
        } else {
            $backupstodelete = array_splice($backupfiles, $tokeep);
            return $backupstodelete;
        }
    }