CRON expression parser that can determine whether or not a CRON expression is
due to run, the next run date and previous run date of a CRON expression. The
determinations made by this class are accurate if checked run once per minute
(seconds are dropped from date time comparisons).
Schedule parts must map to: minute [0-59], hour [0-23], day of month, month
[1-12|JAN-DEC], day of week [1-7|MON-SUN], and an optional year.
Methods summary
public static
Cron\CronExpression
|
#
factory( string $expression, Cron\FieldFactory $fieldFactory = null )
Factory method to create a new CronExpression.
Factory method to create a new CronExpression.
Parameters
- $expression
string $expression The CRON expression to create. There are several special predefined
values which can be used to substitute the CRON expression:
- $fieldFactory
Cron\FieldFactory
$fieldFactory Field factory to use
Returns
Daily
- Run once a day, midnight - 0 0 * * *
Hourly
- Run once an hour, first minute - 0 * * * *
Monthly
- Run once a month, midnight, first of month - 0 0 1 * *
Weekly
- Run once a week, midnight on Sun - 0 0 * * 0
Yearly,
@annually) - Run once a year, midnight, Jan. 1 - 0 0 1 1 *
|
public
|
#
__construct( string $expression, Cron\FieldFactory $fieldFactory )
Parse a CRON expression
Parameters
- $expression
string $expression CRON expression (e.g. '8 * * * *')
- $fieldFactory
Cron\FieldFactory
$fieldFactory Factory to create cron fields
|
public
Cron\CronExpression
|
#
setExpression( string $value )
Set or change the CRON expression
Set or change the CRON expression
Parameters
- $value
string $value CRON expression (e.g. 8 * * * *)
Returns
Throws
InvalidArgumentException if not a valid CRON expression
|
public
Cron\CronExpression
|
#
setPart( integer $position, string $value )
Set part of the CRON expression
Set part of the CRON expression
Parameters
- $position
integer $position The position of the CRON expression to set
- $value
string $value The value to set
Returns
Throws
InvalidArgumentException if the value is not valid for the part
|
public
DateTime
|
#
getNextRunDate( string|DateTime $currentTime = 'now', integer $nth = 0, boolean $allowCurrentDate = false )
Get a next run date relative to the current date or a specific date
Get a next run date relative to the current date or a specific date
Parameters
- $currentTime
string|DateTime $currentTime Relative calculation date
- $nth
integer $nth Number of matches to skip before returning a matching next run date. 0, the
default, will return the current date and time if the next run date falls on the
current date and time. Setting this value to 1 will skip the first match and go
to the second match. Setting this value to 2 will skip the first 2 matches and
so on.
- $allowCurrentDate
boolean $allowCurrentDate Set to TRUE to return the current date if it matches the cron
expression.
Returns
DateTime
Throws
RuntimeException on too many iterations
|
public
DateTime
|
#
getPreviousRunDate( string|DateTime $currentTime = 'now', integer $nth = 0, boolean $allowCurrentDate = false )
Get a previous run date relative to the current date or a specific date
Get a previous run date relative to the current date or a specific date
Parameters
- $currentTime
string|DateTime $currentTime Relative calculation date
- $nth
integer $nth Number of matches to skip before returning
- $allowCurrentDate
boolean $allowCurrentDate Set to TRUE to return the current date if it matches the cron
expression
Returns
DateTime
Throws
RuntimeException on too many iterations
See
|
public
array
|
#
getMultipleRunDates( integer $total, string|DateTime $currentTime = 'now', boolean $invert = false, boolean $allowCurrentDate = false )
Get multiple run dates starting at the current date or a specific date
Get multiple run dates starting at the current date or a specific date
Parameters
- $total
integer $total Set the total number of dates to calculate
- $currentTime
string|DateTime $currentTime Relative calculation date
- $invert
boolean $invert Set to TRUE to retrieve previous dates
- $allowCurrentDate
boolean $allowCurrentDate Set to TRUE to return the current date if it matches the cron
expression
Returns
array Returns an array of run dates
|
public
string|null
|
#
getExpression( string $part = null )
Get all or part of the CRON expression
Get all or part of the CRON expression
Parameters
- $part
string $part Specify the part to retrieve or NULL to get the full cron schedule string.
Returns
string|null Returns the CRON expression, a part of the CRON expression, or NULL if the part
was specified but not found
|
public
string
|
#
__toString( )
Helper method to output the full expression.
Helper method to output the full expression.
Returns
string Full CRON expression
|
public
boolean
|
#
isDue( string|DateTime $currentTime = 'now' )
Determine if the cron is due to run based on the current date or a specific
date. This method assumes that the current number of seconds are irrelevant, and
should be called once per minute.
Determine if the cron is due to run based on the current date or a specific
date. This method assumes that the current number of seconds are irrelevant, and
should be called once per minute.
Parameters
- $currentTime
string|DateTime $currentTime Relative calculation date
Returns
boolean Returns TRUE if the cron is due to run or FALSE if not
|
protected
DateTime
|
#
getRunDate( string|DateTime $currentTime = null, integer $nth = 0, boolean $invert = false, boolean $allowCurrentDate = false )
Get the next or previous run date of the expression relative to a date
Get the next or previous run date of the expression relative to a date
Parameters
- $currentTime
string|DateTime $currentTime Relative calculation date
- $nth
integer $nth Number of matches to skip before returning
- $invert
boolean $invert Set to TRUE to go backwards in time
- $allowCurrentDate
boolean $allowCurrentDate Set to TRUE to return the current date if it matches the cron
expression
Returns
DateTime
Throws
RuntimeException on too many iterations
|