1
|
PHP Cron Expression Parser
|
2
|
==========================
|
3
|
|
4
|
[](https://packagist.org/packages/mtdowling/cron-expression) [](https://packagist.org/packages/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
|
Installing
|
17
|
==========
|
18
|
|
19
|
Add the dependency to your project:
|
20
|
|
21
|
```bash
|
22
|
composer require mtdowling/cron-expression
|
23
|
```
|
24
|
|
25
|
Usage
|
26
|
=====
|
27
|
```php
|
28
|
<?php
|
29
|
|
30
|
require_once '/vendor/autoload.php';
|
31
|
|
32
|
// Works with predefined scheduling definitions
|
33
|
$cron = Cron\CronExpression::factory('@daily');
|
34
|
$cron->isDue();
|
35
|
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');
|
36
|
echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s');
|
37
|
|
38
|
// Works with complex expressions
|
39
|
$cron = Cron\CronExpression::factory('3-59/15 2,6-12 */15 1 2-5');
|
40
|
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');
|
41
|
|
42
|
// Calculate a run date two iterations into the future
|
43
|
$cron = Cron\CronExpression::factory('@daily');
|
44
|
echo $cron->getNextRunDate(null, 2)->format('Y-m-d H:i:s');
|
45
|
|
46
|
// Calculate a run date relative to a specific time
|
47
|
$cron = Cron\CronExpression::factory('@monthly');
|
48
|
echo $cron->getNextRunDate('2010-01-12 00:00:00')->format('Y-m-d H:i:s');
|
49
|
```
|
50
|
|
51
|
CRON Expressions
|
52
|
================
|
53
|
|
54
|
A CRON expression is a string representing the schedule for a particular command to execute. The parts of a CRON schedule are as follows:
|
55
|
|
56
|
* * * * * *
|
57
|
- - - - - -
|
58
|
| | | | | |
|
59
|
| | | | | + year [optional]
|
60
|
| | | | +----- day of week (0 - 7) (Sunday=0 or 7)
|
61
|
| | | +---------- month (1 - 12)
|
62
|
| | +--------------- day of month (1 - 31)
|
63
|
| +-------------------- hour (0 - 23)
|
64
|
+------------------------- min (0 - 59)
|
65
|
|
66
|
Requirements
|
67
|
============
|
68
|
|
69
|
- PHP 5.3+
|
70
|
- PHPUnit is required to run the unit tests
|
71
|
- Composer is required to run the unit tests
|