You are here

public function CronRule::getLastSchedule in Ultimate Cron 7.2

Get last schedule time of rule in UNIX timestamp format.

Return value

int UNIX timestamp of last schedule time.

2 calls to CronRule::getLastSchedule()
CronRule::getNextSchedule in ./CronRule.class.php
Get next schedule time of rule in UNIX timestamp format.
CronRule::isValid in ./CronRule.class.php
Check if a rule is valid.

File

./CronRule.class.php, line 267
This class parses cron rules and determines last execution time using least case integer comparison.

Class

CronRule
@file This class parses cron rules and determines last execution time using least case integer comparison.

Code

public function getLastSchedule() {
  if (isset($this->last_run)) {
    return $this->last_run;
  }

  // Current time round to last minute.
  $time = floor($this->time / 60) * 60;

  // Generate regular expressions from rule.
  $intervals = $this
    ->getIntervals();
  if ($intervals === FALSE) {
    return FALSE;
  }

  // Get starting points.
  $start_year = date('Y', $time);

  // Go back max 28 years (leapyear * weekdays).
  $end_year = $start_year - 28;
  $start_month = date('n', $time);
  $start_day = date('j', $time);
  $start_hour = date('G', $time);
  $start_minute = (int) date('i', $time);

  // If both weekday and days are restricted, then use either or
  // otherwise, use and ... when using or, we have to try out all the days
  // in the month and not just to the ones restricted.
  $check_weekday = count($intervals['weekdays']) != 7;
  $check_both = $check_weekday && count($intervals['days']) != 31;
  $days = $check_both ? range(31, 1) : $intervals['days'];

  // Find last date and time this rule was run.
  for ($year = $start_year; $year > $end_year; $year--) {
    foreach ($intervals['months'] as $month) {
      if ($month < 1 || $month > 12) {
        continue;
      }
      if ($year >= $start_year && $month > $start_month) {
        continue;
      }
      foreach ($days as $day) {
        if ($day < 1 || $day > 31) {
          continue;
        }
        if ($year >= $start_year && $month >= $start_month && $day > $start_day) {
          continue;
        }
        if (!checkdate($month, $day, $year)) {
          continue;
        }

        // Check days and weekdays using and/or logic.
        if ($check_weekday) {
          $date_array = getdate(mktime(0, 0, 0, $month, $day, $year));
          if ($check_both) {
            if (!in_array($day, $intervals['days']) && !isset($intervals['weekdays'][$date_array['wday']])) {
              continue;
            }
          }
          else {
            if (!isset($intervals['weekdays'][$date_array['wday']])) {
              continue;
            }
          }
        }
        if ($day != $start_day || $month != $start_month || $year != $start_year) {
          $start_hour = 23;
          $start_minute = 59;
        }
        foreach ($intervals['hours'] as $hour) {
          if ($hour < 0 || $hour > 23) {
            continue;
          }
          if ($hour > $start_hour) {
            continue;
          }
          if ($hour < $start_hour) {
            $start_minute = 59;
          }
          foreach ($intervals['minutes'] as $minute) {
            if ($minute < 0 || $minute > 59) {
              continue;
            }
            if ($minute > $start_minute) {
              continue;
            }
            break 5;
          }
        }
      }
    }
  }
  if (!isset($hour) || !isset($minute) || !isset($month) || !isset($day) || !isset($year)) {
    return FALSE;
  }

  // Create UNIX timestamp from derived date+time.
  $this->last_run = mktime($hour, $minute, 0, $month, $day, $year);
  return $this->last_run;
}