Calculate when a cron job will be executed then next time

后端 未结 8 928
庸人自扰
庸人自扰 2020-12-02 08:19

I have a cron \"time definition\"

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          


        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 08:52

    Here's a PHP project that is based on dlamblin's psuedo code.

    It can calculate the next run date of a CRON expression, the previous run date of a CRON expression, and determine if a CRON expression matches a given time. You can skip This CRON expression parser fully implements CRON:

    1. Increments of ranges (e.g. */12, 3-59/15)
    2. Intervals (e.g. 1-4, MON-FRI, JAN-MAR )
    3. Lists (e.g. 1,2,3 | JAN,MAR,DEC)
    4. Last day of a month (e.g. L)
    5. Last given weekday of a month (e.g. 5L)
    6. Nth given weekday of a month (e.g. 3#2, 1#1, MON#4)
    7. Closest weekday to a given day of the month (e.g. 15W, 1W, 30W)

    https://github.com/mtdowling/cron-expression

    Usage (PHP 5.3+):

    isDue();
    $cron->getNextRunDate();
    $cron->getPreviousRunDate();
    
    // Works with complex expressions
    $cron = Cron\CronExpression::factory('15 2,6-12 */15 1 2-5');
    $cron->getNextRunDate();
    

提交回复
热议问题