function CronRule::getLastRan in Ultimate Cron 7
Same name and namespace in other branches
- 8 CronRule.class.php \CronRule::getLastRan()
- 6 CronRule.class.php \CronRule::getLastRan()
Get last execution time of rule in unix timestamp format
Parameters
$time: (int) time to use as relative time (default now)
Return value
(int) unix timestamp of last execution time
1 call to CronRule::getLastRan()
- CronRule::isValid in ./
CronRule.class.php - Check if a rule is valid
File
- ./
CronRule.class.php, line 169 - This class parses cron rules and determines last execution time using least case integer comparison.
Class
- CronRule
- @file
Code
function getLastRan($time = NULL) {
// Current time round to last minute
if (!isset($time)) {
$time = time();
}
$time = floor($time / 60) * 60;
// Generate regular expressions from rule
$intervals = $this
->getIntervals();
if ($intervals === FALSE) {
return FALSE;
}
// Get starting points
$start_year = date('Y', $time);
$end_year = $start_year - 28;
// Go back max 28 years (leapyear * weekdays)
$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_both = count($intervals['days']) != 31 && count($intervals['weekdays']) != 7 ? FALSE : TRUE;
$days = $check_both ? $intervals['days'] : range(31, 1);
// 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
$date_array = getdate(mktime(0, 0, 0, $month, $day, $year));
if ($check_both) {
if (!isset($intervals['weekdays'][$date_array['wday']])) {
continue;
}
}
else {
if (!in_array($day, $intervals['days']) && !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;
}
}
}
}
}
// Create unix timestamp from derived date+time
$time = mktime($hour, $minute, 0, $month, $day, $year);
return $time;
}