public static function calculate_next_automated_backup($ignoredtimezone, $now) {

        $config = get_config('backup');

        $backuptime = new DateTime('@' . $now);
        $backuptime->setTimezone(core_date::get_server_timezone_object());
        $backuptime->setTime($config->backup_auto_hour, $config->backup_auto_minute);

        while ($backuptime->getTimestamp() < $now) {
            $backuptime->add(new DateInterval('P1D'));
        }

        // Get number of days from backup date to execute backups.
        $automateddays = substr($config->backup_auto_weekdays, $backuptime->format('w')) . $config->backup_auto_weekdays;
        $daysfromnow = strpos($automateddays, "1");

        // Error, there are no days to schedule the backup for.
        if ($daysfromnow === false) {
            return 0;
        }

        if ($daysfromnow > 0) {
            $backuptime->add(new DateInterval('P' . $daysfromnow . 'D'));
        }

        return $backuptime->getTimestamp();
    }