Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1 * * * * (every hour at xx:01)
- 2 5 * * * (every day at 05:02)
- 0 4 3 * * (every third day of the month at 04:00)
- * 2 * * 5 (every minute between 02:00 and 02:59 on fridays)
- //Totaly made up language
- next = getTimeNow();
- next.addMinutes(1) //so that next is never now
- done = false;
- while (!done) {
- if (cron.minute != '*' && next.minute != cron.minute) {
- if (next.minute > cron.minute) {
- next.addHours(1);
- }
- next.minute = cron.minute;
- }
- if (cron.hour != '*' && next.hour != cron.hour) {
- if (next.hour > cron.hour) {
- next.hour = cron.hour;
- next.addDays(1);
- next.minute = 0;
- continue;
- }
- next.hour = cron.hour;
- next.minute = 0;
- continue;
- }
- if (cron.weekday != '*' && next.weekday != cron.weekday) {
- deltaDays = cron.weekday - next.weekday //assume weekday is 0=sun, 1 ... 6=sat
- if (deltaDays < 0) { deltaDays+=7; }
- next.addDays(deltaDays);
- next.hour = 0;
- next.minute = 0;
- continue;
- }
- if (cron.day != '*' && next.day != cron.day) {
- if (next.day > cron.day || !next.month.hasDay(cron.day)) {
- next.addMonths(1);
- next.day = 1; //assume days 1..31
- next.hour = 0;
- next.minute = 0;
- continue;
- }
- next.day = cron.day
- next.hour = 0;
- next.minute = 0;
- continue;
- }
- if (cron.month != '*' && next.month != cron.month) {
- if (next.month > cron.month) {
- next.addMonths(12-next.month+cron.month)
- next.day = 1; //assume days 1..31
- next.hour = 0;
- next.minute = 0;
- continue;
- }
- next.month = cron.month;
- next.day = 1;
- next.hour = 0;
- next.minute = 0;
- continue;
- }
- done = true;
- }
- //Shorter more loopy version
- next = getTimeNow().addMinutes(1);
- while (true) {
- if (cron.month != '*' && next.month != cron.month) {
- next.addMonths(1);
- next.day = 1;
- next.hour = 0;
- next.minute = 0;
- continue;
- }
- if (cron.day != '*' && next.day != cron.day) {
- next.addDays(1);
- next.hour = 0;
- next.minute = 0;
- continue;
- }
- if (cron.weekday != '*' && next.weekday != cron.weekday) {
- next.addDays(1);
- next.hour = 0;
- next.minute = 0;
- continue;
- }
- if (cron.hour != '*' && next.hour != cron.hour) {
- next.addHours(1);
- next.minute = 0;
- continue;
- }
- if (cron.minute != '*' && next.minute != cron.minute) {
- next.addMinutes(1);
- continue;
- }
- break;
- }
- <?php
- // Works with predefined scheduling definitions
- $cron = CronCronExpression::factory('@daily');
- $cron->isDue();
- $cron->getNextRunDate();
- $cron->getPreviousRunDate();
- // Works with complex expressions
- $cron = CronCronExpression::factory('15 2,6-12 */15 1 2-5');
- $cron->getNextRunDate();
- class myMiniDate {
- var $myTimestamp;
- static private $dateComponent = array(
- 'second' => 's',
- 'minute' => 'i',
- 'hour' => 'G',
- 'day' => 'j',
- 'month' => 'n',
- 'year' => 'Y',
- 'dow' => 'w',
- 'timestamp' => 'U'
- );
- static private $weekday = array(
- 1 => 'monday',
- 2 => 'tuesday',
- 3 => 'wednesday',
- 4 => 'thursday',
- 5 => 'friday',
- 6 => 'saturday',
- 0 => 'sunday'
- );
- function __construct($ts = NULL) { $this->myTimestamp = is_null($ts)?time():$ts; }
- function __set($var, $value) {
- list($c['second'], $c['minute'], $c['hour'], $c['day'], $c['month'], $c['year'], $c['dow']) = explode(' ', date('s i G j n Y w', $this->myTimestamp));
- switch ($var) {
- case 'dow':
- $this->myTimestamp = strtotime(self::$weekday[$value], $this->myTimestamp);
- break;
- case 'timestamp':
- $this->myTimestamp = $value;
- break;
- default:
- $c[$var] = $value;
- $this->myTimestamp = mktime($c['hour'], $c['minute'], $c['second'], $c['month'], $c['day'], $c['year']);
- }
- }
- function __get($var) {
- return date(self::$dateComponent[$var], $this->myTimestamp);
- }
- function modify($how) { return $this->myTimestamp = strtotime($how, $this->myTimestamp); }
- }
- $cron = new myMiniDate(time() + 60);
- $cron->second = 0;
- $done = 0;
- echo date('Y-m-d H:i:s') . '<hr>' . date('Y-m-d H:i:s', $cron->timestamp) . '<hr>';
- $Job = array(
- 'Minute' => 5,
- 'Hour' => 3,
- 'Day' => 13,
- 'Month' => null,
- 'DOW' => 5,
- );
- while ($done < 100) {
- if (!is_null($Job['Minute']) && ($cron->minute != $Job['Minute'])) {
- if ($cron->minute > $Job['Minute']) {
- $cron->modify('+1 hour');
- }
- $cron->minute = $Job['Minute'];
- }
- if (!is_null($Job['Hour']) && ($cron->hour != $Job['Hour'])) {
- if ($cron->hour > $Job['Hour']) {
- $cron->modify('+1 day');
- }
- $cron->hour = $Job['Hour'];
- $cron->minute = 0;
- }
- if (!is_null($Job['DOW']) && ($cron->dow != $Job['DOW'])) {
- $cron->dow = $Job['DOW'];
- $cron->hour = 0;
- $cron->minute = 0;
- }
- if (!is_null($Job['Day']) && ($cron->day != $Job['Day'])) {
- if ($cron->day > $Job['Day']) {
- $cron->modify('+1 month');
- }
- $cron->day = $Job['Day'];
- $cron->hour = 0;
- $cron->minute = 0;
- }
- if (!is_null($Job['Month']) && ($cron->month != $Job['Month'])) {
- if ($cron->month > $Job['Month']) {
- $cron->modify('+1 year');
- }
- $cron->month = $Job['Month'];
- $cron->day = 1;
- $cron->hour = 0;
- $cron->minute = 0;
- }
- $done = (is_null($Job['Minute']) || $Job['Minute'] == $cron->minute) &&
- (is_null($Job['Hour']) || $Job['Hour'] == $cron->hour) &&
- (is_null($Job['Day']) || $Job['Day'] == $cron->day) &&
- (is_null($Job['Month']) || $Job['Month'] == $cron->month) &&
- (is_null($Job['DOW']) || $Job['DOW'] == $cron->dow)?100:($done+1);
- }
- echo date('Y-m-d H:i:s', $cron->timestamp) . '<hr>';
- function parse_crontab($time, $crontab)
- {$time=explode(' ', date('i G j n w', strtotime($time)));
- $crontab=explode(' ', $crontab);
- foreach ($crontab as $k=>&$v)
- {$v=explode(',', $v);
- foreach ($v as &$v1)
- {$v1=preg_replace(array('/^*$/', '/^d+$/', '/^(d+)-(d+)$/', '/^*/(d+)$/'),
- array('true', $time[$k].'=== ', '(1<='.$time[$k].' and '.$time[$k].'<=2)', $time[$k].'%1===0'),
- $v1
- );
- }
- $v='('.implode(' or ', $v).')';
- }
- $crontab=implode(' and ', $crontab);
- return eval('return '.$crontab.';');
- }
- var_export(parse_crontab('2011-05-04 02:08:03', '*/2,3-5,9 2 3-5 */2 *'));
- var_export(parse_crontab('2011-05-04 02:08:03', '*/8 */2 */4 */5 *'));
Add Comment
Please, Sign In to add comment