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:
- Increments of ranges (e.g. */12, 3-59/15)
- Intervals (e.g. 1-4, MON-FRI, JAN-MAR )
- Lists (e.g. 1,2,3 | JAN,MAR,DEC)
- Last day of a month (e.g. L)
- Last given weekday of a month (e.g. 5L)
- Nth given weekday of a month (e.g. 3#2, 1#1, MON#4)
- Closest weekday to a given day of the month (e.g. 15W, 1W, 30W)
https://github.com/mtdowling/cron-expression
Usage (PHP 5.3+):
<?php
// Works with predefined scheduling definitions
$cron = Cron\CronExpression::factory('@daily');
$cron->isDue();
$cron->getNextRunDate();
$cron->getPreviousRunDate();
// Works with complex expressions
$cron = Cron\CronExpression::factory('15 2,6-12 */15 1 2-5');
$cron->getNextRunDate();