Znalazłem diyyzm miał świetną odpowiedź, ale znalazłem kluczowy błąd.
Jeśli wpiszesz czas cron, taki jak 0 * * * *
, będzie on działał w 0 minucie, 8, minucie i 9 minucie. Kod podaje warunkowe 08===0
, które zwraca wartość true, ponieważ PHP interpretuje liczby rozpoczynające się od 0 jako ósemkowe, a 08 i 09 nie są poprawnymi liczbami ósemkowymi, więc są interpretowane jako 0. Więcej informacji tutaj.
How to prevent PHP from doing octal math in conditionals? (why does 08 === 0)
Oto stałe i dobrze skomentował wersja kodu diyism użytkownika.
// Parse CRON frequency
function parse_crontab($time, $crontab) {
// Get current minute, hour, day, month, weekday
$time = explode(' ', date('i G j n w', strtotime($time)));
// Split crontab by space
$crontab = explode(' ', $crontab);
// Foreach part of crontab
foreach ($crontab as $k => &$v) {
// Remove leading zeros to prevent octal comparison, but not if number is already 1 digit
$time[$k] = preg_replace('/^0+(?=\d)/', '', $time[$k]);
// 5,10,15 each treated as seperate parts
$v = explode(',', $v);
// Foreach part we now have
foreach ($v as &$v1) {
// Do preg_replace with regular expression to create evaluations from crontab
$v1 = preg_replace(
// Regex
array(
// *
'/^\*$/',
// 5
'/^\d+$/',
// 5-10
'/^(\d+)\-(\d+)$/',
// */5
'/^\*\/(\d+)$/'
),
// Evaluations
// trim leading 0 to prevent octal comparison
array(
// * is always true
'true',
// Check if it is currently that time,
$time[$k] . '===\0',
// Find if more than or equal lowest and lower or equal than highest
'(\1<=' . $time[$k] . ' and ' . $time[$k] . '<=\2)',
// Use modulus to find if true
$time[$k] . '%\1===0'
),
// Subject we are working with
$v1
);
}
// Join 5,10,15 with `or` conditional
$v = '(' . implode(' or ', $v) . ')';
}
// Require each part is true with `and` conditional
$crontab = implode(' and ', $crontab);
// Evaluate total condition to find if true
return eval('return ' . $crontab . ';');
}
http://stackoverflow.com/questions/321494/calculate-when-a-cron-job- will-be-executed-then-next-time/3453872#3453872 –