Cron
Cron
This class can be used to parse cron strings and compute schedules.
It can parse a given string with a schedule specification in cron format. The class can compute the last and the next schedule times relative to a given time.
Example
$cron = new \Hazaar\Cron('0,30 9-17 \* \* 1-5');
$next = $cron->getNextOccurrence();This will get the next occurrence from the schedule which should return dates and times for every 0th and 30th minute between 9am and 5pm, Monday to Friday.
Properties
ranges
Ranges.
private $ranges = array (
'IDX_MINUTE' =>
array (
'min' => 0,
'max' => 59,
'name' => 'i',
),
0 => NULL,
'IDX_HOUR' =>
array (
'min' => 0,
'max' => 23,
'name' => 'G',
),
1 => NULL,
'IDX_DAY' =>
array (
'min' => 1,
'max' => 31,
'name' => 'd',
),
2 => NULL,
'IDX_MONTH' =>
array (
'min' => 1,
'max' => 12,
'name' => 'm',
),
3 => NULL,
'IDX_WEEKDAY' =>
array (
'min' => 0,
'max' => 7,
'name' => 'w',
),
4 => NULL,
)intervals
Named intervals.
private $intervals = array (
'@yearly' => '0 0 1 1 *',
'@annualy' => '0 0 1 1 *',
'@monthly' => '0 0 1 * *',
'@weekly' => '0 0 * * 0',
'@midnight' => '0 0 * * *',
'@daily' => '0 0 * * *',
'@hourly' => '0 * * * *',
'@reboot' => 'now',
)keywords
Possible keywords for months/weekdays.
private $keywords = array (
'IDX_MONTH' =>
array (
'/(january|januar|jan)/i' => 1,
'/(february|februar|feb)/i' => 2,
'/(march|maerz|m�rz|mar|mae|m�r)/i' => 3,
'/(april|apr)/i' => 4,
'/(may|mai)/i' => 5,
'/(june|juni|jun)/i' => 6,
'/(july|juli|jul)/i' => 7,
'/(august|aug)/i' => 8,
'/(september|sep)/i' => 9,
'/(october|oktober|okt|oct)/i' => 10,
'/(november|nov)/i' => 11,
'/(december|dezember|dec|dez)/i' => 12,
),
'IDX_WEEKDAY' =>
array (
'/(sunday|sonntag|sun|son|su|so)/i' => 0,
'/(monday|montag|mon|mo)/i' => 1,
'/(tuesday|dienstag|die|tue|tu|di)/i' => 2,
'/(wednesdays|mittwoch|mit|wed|we|mi)/i' => 3,
'/(thursday|donnerstag|don|thu|th|do)/i' => 4,
'/(friday|freitag|fre|fri|fr)/i' => 5,
'/(saturday|samstag|sam|sat|sa)/i' => 6,
),
)pcron
public int $pcronMethods
__construct
Creates a new instance of the Cron class.
public __construct(string $expression): voidParameters
| Parameter | Type | Description |
|---|---|---|
$expression | string | The CRON expression to parse |
getNextOccurrence
Calculates the next time and date based on the supplied expression.
public getNextOccurrence(?int $timestamp): ?intIf a reference-time is passed, the next time and date after that time is calculated.
Parameters
| Parameter | Type | Description |
|---|---|---|
$timestamp | int | optional reference-time |
getLastOccurrence
Calculates the last time and date before the supplied expression.
public getLastOccurrence(?int $timestamp): ?intIf a reference-time is passed, the last time and date before that time is calculated.
Parameters
| Parameter | Type | Description |
|---|---|---|
$timestamp | int | optional reference-time |
calculateDateTime
Calculates the time and date at which the next/last call of a cronjob is/was due.
private calculateDateTime(array $rtime, bool $next = true): ?intParameters
| Parameter | Type | Description |
|---|---|---|
$rtime | array | reference-time |
$next | bool | true = nextOccurence, false = lastOccurence |
getTimestamp
Converts an unix-timestamp to an array.
private getTimestamp(?int $timestamp): voidThe returned array contains the following values:
[0] -> minute [1] -> hour [2] -> day [3] -> month [4] -> weekday [5] -> year
The array is used by various functions.
Parameters
| Parameter | Type | Description |
|---|---|---|
$timestamp | int | If none is given, the current time is used |
findValue
Checks if the given value exists in an array.
private findValue(int $value, mixed $data, boolean $next = true): voidIf it does not exist, the next higher/lower value is returned (depending on $next). If no higher/lower value exists, false is returned.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | int | |
$data | mixed | |
$next | boolean |
arrayReverse
Reverses all sub-arrays of our cron array.
private arrayReverse(array $cron): voidThe reversed values are used for calculations that are run when getLastOccurence() is called.
Parameters
| Parameter | Type | Description |
|---|---|---|
$cron | array |
parse
Analyses crontab-expressions like "* * 1,2,3 * mon,tue" and returns an array containing all values.
private parse(string $expression): mixedIf it can not be parsed then it returns FALSE
Parameters
| Parameter | Type | Description |
|---|---|---|
$expression | string | the cron-expression to parse |
expandSegment
Analyses a single segment.
private expandSegment(int $idx, string $segment, false $): voidParameters
| Parameter | Type | Description |
|---|---|---|
$idx | int | |
$segment | string | |
$ | false |
parseAtom
Analyses a single segment.
private parseAtom(string $atom): voidParameters
| Parameter | Type | Description |
|---|---|---|
$atom | string | The segment to parse |
Generated by Hazaar API Doc Generator on Mon, 27 Oct 2025 13:01:29 +0000