mike-tralala / cron-expression
A library to use cron expressions in PHP
Installs: 1 045
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.12@dev
- phpunit/phpunit: 6.5.x-dev
This package is not auto-updated.
Last update: 2024-09-24 08:43:06 UTC
README
This cron-expression
library can parse cron expressions like * * * * *
and check if
it is due or not. You are also able to determine the next or previous running dates.
This parser can handle anything the normal cron syntax accepts. with 1 exception and that are the named constructs (e.g. mon-thu or jan-dec).
Installing
Add the dependency to your project:
composer require mike-tralala/cron-expression
Usage
<?php require_once __DIR__ . '/vendor/autoload.php'; use MikeTralala\CronExpression\Expression\DueDateCalculator; use MikeTralala\CronExpression\Expression\Expression; // Works with predefined definitions $expression = new Expression(Expression::WEEKLY); $expression->isDue(); $expression = new Expression(Expression::MINUTELY); $expression->isDue(); // Works with complex definitions $expression = new Expression('10-40/10 0 */5 1 4,5'); $expression->isDue(); // Calculating next running date $calculator = new DueDateCalculator(); $expression = new Expression('*/2 * * * *'); $dueDates = $calculator->getNextDueDates($expression, 5, \DateTime::createFromFormat('Y-m-d H:i', '2018-01-01 00:00')); echo $expression . PHP_EOL; foreach ($dueDates as $dueDate) { echo $dueDate->format('Y-m-d H:i') . PHP_EOL; } // Output: // */2 * * * * // 2018-01-01 00:00 // 2018-01-01 00:02 // 2018-01-01 00:04 // 2018-01-01 00:06 // 2018-01-01 00:08
CRON Expressions
A CRON expression is a string representing the schedule for a particular command to execute. The parts of a CRON schedule are as follows:
* * * * *
- - - - -
| | | | |
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +---------- month (1 - 12)
| | +--------------- day of month (1 - 31)
| +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)
Requirements
- PHP 5.5+
- PHPUnit is required to run the unit tests
- Composer is required to run the unit tests