adhocore / php-cron-expr

Ultra lightweight, Dependency free and Super Fast Cron Expression parser for PHP
https://github.com/adhocore/php-cron-expr
MIT License
50 stars 5 forks source link
adhocore cron cron-expr cron-expression cron-parser cron-relay crontab crontab-parser php-cron php-crontab scheduler

adhocore/cron-expr

Latest Version Travis Build Scrutinizer CI Codecov branch StyleCI Software License Tweet Support

Installation

composer require adhocore/cron-expr

# PHP5.6 or lower
composer require adhocore/cron-expr:0.1.0

Usage

Basic

use Ahc\Cron\Expression;
use Ahc\Cron\Normalizer;

Expression::isDue('@always');
Expression::isDue(Normalizer::HOURLY, '2015-01-01 00:00:00');
Expression::isDue('*/20 * * * *', new DateTime);
Expression::isDue('5-34/4 * * * *', time());

// Dont like static calls? Below is possible too!
$expr = new Expression;
$expr->isCronDue('*/1 * * * *', time());

Bulk checks

When checking for several jobs at once, if more than one of the jobs share equivalent expression then the evaluation is done only once per go thus greatly improving performnce.

use Ahc\Cron\Expression;

$jobs = [
    'job1' => '*/2 */2 * * *',
    'job1' => '* 20,21,22 * * *',
    'job3' => '7-9 * */9 * *',
    'job4' => '*/5 * * * *',
    'job5' => '@5minutes',     // equivalent to job4 (so it is due if job4 is due)
    'job6' => '7-9 * */9 * *', // exact same as job3 (so it is due if job3 is due)
];

// The second param $time can be used same as above: null/time()/date string/DateTime
$dues = Expression::getDues($jobs, '2015-08-10 21:50:00');
// ['job1', 'job4', 'job5']

// Dont like static calls? Below is possible too!
$expr = new Expression;
$dues = $expr->filter($jobs, time());

Cron Expression

Cron expression normally consists of 5 segments viz:

<minute> <hour> <day> <month> <weekday>

and sometimes there can be 6th segment for <year> at the end.

Real Abbreviations

You can use real abbreviations for month and week days. eg: JAN, dec, fri, SUN

Tags

Following tags are available and they are converted to real cron expressions before parsing:

You can refer them with constants from Ahc\Cron\Normalizer like Ahc\Cron\Normalizer::WEEKLY

Modifiers

Following modifiers supported

LICENSE

© MIT | 2017-2019, Jitendra Adhikari

Credits

This project is release managed by please.