You are here

public function CronRule::getNextSchedule in Ultimate Cron 8.2

Get next schedule time of rule in UNIX timestamp format.

Return value

integer UNIX timestamp of next schedule time.

File

src/CronRule.php, line 383

Class

CronRule

Namespace

Drupal\ultimate_cron

Code

public function getNextSchedule() {
  if (isset($this->next_run)) {
    return $this->next_run;
  }
  $intervals = $this
    ->getIntervals();
  $last_schedule = $this
    ->getLastSchedule();
  $next['minutes'] = (int) date('i', $last_schedule);
  $next['hours'] = date('G', $last_schedule);
  $next['days'] = date('j', $last_schedule);
  $next['months'] = date('n', $last_schedule);
  $year = date('Y', $last_schedule);
  $check_weekday = count($intervals['weekdays']) != 7;
  $check_both = $check_weekday && count($intervals['days']) != 31 ? TRUE : FALSE;
  $days = $intervals['days'];
  $intervals['days'] = $check_both ? range(31, 1) : $intervals['days'];
  $ranges = self::$ranges;
  unset($ranges['weekdays']);
  foreach ($ranges as $type => $range) {
    $found = array_keys($intervals[$type], $next[$type]);
    $idx[$type] = reset($found);
  }
  reset($ranges);
  while ($type = key($ranges)) {
    next($ranges);
    $idx[$type]--;
    if ($idx[$type] < 0) {
      $found = array_keys($intervals[$type], end($intervals[$type]));
      $idx[$type] = reset($found);
      if ($type == 'months') {
        $year--;
        reset($ranges);
      }
      continue;
    }
    if ($type == 'days' && $check_weekday) {

      // Check days and weekdays using and/or logic.
      $date_array = getdate(mktime($intervals['hours'][$idx['hours']], $intervals['minutes'][$idx['minutes']], 0, $intervals['months'][$idx['months']], $intervals['days'][$idx['days']], $year));
      if ($check_both) {
        if (!in_array($intervals['days'][$idx['days']], $days) && !isset($intervals['weekdays'][$date_array['wday']])) {
          reset($ranges);
          next($ranges);
          next($ranges);
          continue;
        }
      }
      else {
        if (!isset($intervals['weekdays'][$date_array['wday']])) {
          reset($ranges);
          next($ranges);
          next($ranges);
          continue;
        }
      }
    }
    break;
  }
  $this->next_run = mktime($intervals['hours'][$idx['hours']], $intervals['minutes'][$idx['minutes']], 0, $intervals['months'][$idx['months']], $intervals['days'][$idx['days']], $year);
  return $this->next_run;
}