Fixed On cron save/edit and add-on install/upgrade: `xf_cron_entry`.`next_run` calculated wrong, if "any" minute, but only certain hours are selected

nocte

Well-known member
Licensed customer
Affected version
2.3.10
This will only affect you if you are saving a new cron or edit it outside of the hour(s) is should run in later. It also occurs, when updating (and most likely) installing an add-on, no matter if the cron has changed or not. After the first routine run of such a cron job (see below), I assume everything should work as expected.

Here's a screenshot as example:

Bildschirmfoto 2026-05-25 um 00.33.50.webp

Before hour 0 UTC the column next_run in the xf_cron_entry database table, should show minute 0.

But XF is not fully prepared for this use case, so it shows after saving the cron entry the right hour, but the "current minute + 1" as "next run".

Screenshot (my timestamp 02:38 is UTC 00:38 and 2 hours from now; real next run should be at 02:00 my timezone):

Bildschirmfoto 2026-05-25 um 00.39.56.webp

In the CronEntry entity we have in the _preSave() method this line:
PHP:
$this->set('next_run', $this->calculateNextRun());

This leads us to this code in the CalculateNextRunService:

PHP:
    public function calculateNextRunTime(array $runRules, $currentTime = null)
    {
        $currentTime = ($currentTime ?? \XF::$time);
        $nextRun = new \DateTime('@' . $currentTime);
        $nextRun->modify('+1 minute');
        if (empty($runRules['minutes']))
        {
            $runRules['minutes'] = [-1];
        }
        $this->modifyRunTimeMinutes($runRules['minutes'], $nextRun);
        if (empty($runRules['hours']))
        {
            $runRules['hours'] = [-1];
        }
        $this->modifyRunTimeHours($runRules['hours'], $nextRun);
        if (!empty($runRules['day_type']))
        {
            if ($runRules['day_type'] == 'dow')
            {
                if (empty($runRules['dow']))
                {
                    $runRules['dow'] = [-1];
                }
                $this->modifyRunTimeDayOfWeek($runRules['dow'], $nextRun);
            }
            else
            {
                if (empty($runRules['dom']))
                {
                    $runRules['dom'] = [-1];
                }
                $this->modifyRunTimeDayOfMonth($runRules['dom'], $nextRun);
            }
        }
        return intval($nextRun->format('U'));
    }

and:

PHP:
    protected function modifyRunTimeMinutes(array $minuteRules, \DateTime &$nextRun)
    {
        $currentMinute = $nextRun->format('i');
        $this->modifyRunTimeUnits($minuteRules, $nextRun, $currentMinute, 'minute', 'hour');
    }

I have no suggested fix for this for now..
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.3.11).

Change log:
Fix hour-constrained crons being scheduled at the wrong minute offset
There may be a delay before changes are rolled out to the XenForo Community.
 
Back
Top Bottom