| 1 |
PHP Cron Expression Parser |
| 2 |
========================== |
| 3 |
|
| 4 |
[](https://packagist.org/packages/mtdowling/cron-expression](https://packagist.org/packages/mtdowling/cron-expression](https://packagist.org/packages/mtdowling/cron-expression) [](https://packagist.org/packages/mtdowling/cron-expression](https://packagist.org/packages/mtdowling/cron-expression](https://packagist.org/packages/mtdowling/cron-expression) [](http://travis-ci.org/mtdowling/cron-expression](http://travis-ci.org/mtdowling/cron-expression](http://travis-ci.org/mtdowling/cron-expression) |
| 5 |
|
| 6 |
The PHP cron expression parser can parse a CRON expression, determine if it is |
| 7 |
due to run, calculate the next run date of the expression, and calculate the previous |
| 8 |
run date of the expression. You can calculate dates far into the future or past by |
| 9 |
skipping n number of matching dates. |
| 10 |
|
| 11 |
The parser can handle increments of ranges (e.g. */12, 2-59/3), intervals (e.g. 0-9), |
| 12 |
lists (e.g. 1,2,3), W to find the nearest weekday for a given day of the month, L to |
| 13 |
find the last day of the month, L to find the last given weekday of a month, and hash |
| 14 |
(#) to find the nth weekday of a given month. |
| 15 |
|
| 16 |
Credits |
| 17 |
========== |
| 18 |
|
| 19 |
Created by Micheal Dowling. Ported to PHP 5.2 by Flightless, Inc. |
| 20 |
Based on version 1.0.3: https://github.com/mtdowling/cron-expression/tree/v1.0.3 |
| 21 |
|
| 22 |
Installing |
| 23 |
========== |
| 24 |
|
| 25 |
Add the following to your project's composer.json: |
| 26 |
|
| 27 |
```javascript |
| 28 |
{ |
| 29 |
"require": { |
| 30 |
"mtdowling/cron-expression": "1.0.*" |
| 31 |
} |
| 32 |
} |
| 33 |
``` |
| 34 |
|
| 35 |
Usage |
| 36 |
===== |
| 37 |
```php |
| 38 |
<?php |
| 39 |
|
| 40 |
require_once '/vendor/autoload.php'; |
| 41 |
|
| 42 |
// Works with predefined scheduling definitions |
| 43 |
$cron = Cron\CronExpression::factory('@daily'); |
| 44 |
$cron->isDue(); |
| 45 |
echo $cron->getNextRunDate()->format('Y-m-d H:i:s'); |
| 46 |
echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s'); |
| 47 |
|
| 48 |
// Works with complex expressions |
| 49 |
$cron = Cron\CronExpression::factory('3-59/15 2,6-12 */15 1 2-5'); |
| 50 |
echo $cron->getNextRunDate()->format('Y-m-d H:i:s'); |
| 51 |
|
| 52 |
// Calculate a run date two iterations into the future |
| 53 |
$cron = Cron\CronExpression::factory('@daily'); |
| 54 |
echo $cron->getNextRunDate(null, 2)->format('Y-m-d H:i:s'); |
| 55 |
|
| 56 |
// Calculate a run date relative to a specific time |
| 57 |
$cron = Cron\CronExpression::factory('@monthly'); |
| 58 |
echo $cron->getNextRunDate('2010-01-12 00:00:00')->format('Y-m-d H:i:s'); |
| 59 |
``` |
| 60 |
|
| 61 |
CRON Expressions |
| 62 |
================ |
| 63 |
|
| 64 |
A CRON expression is a string representing the schedule for a particular command to execute. The parts of a CRON schedule are as follows: |
| 65 |
|
| 66 |
* * * * * * |
| 67 |
- - - - - - |
| 68 |
| | | | | | |
| 69 |
| | | | | + year [optional] |
| 70 |
| | | | +----- day of week (0 - 7) (Sunday=0 or 7) |
| 71 |
| | | +---------- month (1 - 12) |
| 72 |
| | +--------------- day of month (1 - 31) |
| 73 |
| +-------------------- hour (0 - 23) |
| 74 |
+------------------------- min (0 - 59) |
| 75 |
|
| 76 |
Requirements |
| 77 |
============ |
| 78 |
|
| 79 |
- PHP 5.3+ |
| 80 |
- PHPUnit is required to run the unit tests |
| 81 |
- Composer is required to run the unit tests |
| 82 |
|
| 83 |
CHANGELOG |
| 84 |
========= |
| 85 |
|
| 86 |
1.0.3 (2013-11-23) |
| 87 |
------------------ |
| 88 |
|
| 89 |
* Only set default timezone if the given $currentTime is not a DateTime instance (#34) |
| 90 |
* Fixes issue #28 where PHP increments of ranges were failing due to PHP casting hyphens to 0 |
| 91 |
* Now supports expressions with any number of extra spaces, tabs, or newlines |
| 92 |
* Using static instead of self in `CronExpression::factory` |
| 93 |
|