0) { return $nextrun; } else { return 0; } } /** * Calculates the next task run time today after a specified hour and minute. * * @param array - Array of the cron task data from the database result. * @param int - Hour (-2 for current hour, -1 for every hour) * @param int - Minute (-2 for current minute, -1 for every minute) */ function cron_next_run($cronstuff, $hour = -2, $minute = -2) { if ($hour == -2) { $hour = date('H', TIME); } if ($minute == -2) { $minute = date('i', TIME); } // allow the minute value to be a serialized array, to execute tasks // on more than one occasion during an hour (e.g. every 15 mins) if (!is_numeric($cronstuff['minute'])) { $cronstuff['minute'] = unserialize($cronstuff['minute']); } else { $cronstuff['minute'] = array(0 => $cronstuff['minute']); } // every hour and minute if ($cronstuff['hour'] == -1 && $cronstuff['minute'][0] == -1) { $newstuff['hour'] = $hour; $newstuff['minute'] = $minute + 1; } // every hour, specific minutes elseif ($cronstuff['hour'] == -1 && $cronstuff['minute'][0] != -1) { $newstuff['hour'] = $hour; $nextminute = cron_next_minute($cronstuff['minute'], $minute); if ($nextminute === false) { // impossible to do this hour, skip to the next ++$newstuff['hour']; $nextminute = $cronstuff['minute'][0]; } $newstuff['minute'] = $nextminute; } // every minute during some hours elseif ($cronstuff['hour'] != -1 && $cronstuff['minute'][0] == -1) { if ($cronstuff['hour'] < $hour) { // too late to run the task today $newstuff['hour'] = -1; $newstuff['minute'] = -1; } elseif ($cronstuff['hour'] == $hour) { // this hour $newstuff['hour'] = $cronstuff['hour']; $newstuff['minute'] = $minute + 1; } else { // some time in future, schedule check at 0th minute $newstuff['hour'] = $cronstuff['hour']; $newstuff['minute'] = 0; } } // some other time elseif ($cronstuff['hour'] != -1 && $cronstuff['minute'][0] != -1) { $nextminute = fetch_next_minute($cronstuff['minute'], $minute); if ($cronstuff['hour'] < $hour || ($cronstuff['hour'] == $hour && $nextminute === false) ) { // it's not going to run today $newstuff['hour'] = -1; $newstuff['minute'] = -1; } else { $newstuff['hour'] = $cronstuff['hour']; $newstuff['minute'] = $nextminute; } } return $newstuff; } /** * Loops through an array of numbers and returns the first number * greater than the second parameter. * * @param array - An array of numerical values * @param int - A number to compare against the array values * @return int - First greater number found */ function cron_next_minute($minutearray, $minute) { foreach ($minutearray as $nextminute) { if ($nextminute > $minute) { return $nextminute; } } return false; } ?>