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.
- Full name:
\Hazaar\Cron
Properties
ranges
Ranges.
private array $ranges
intervals
Named intervals.
private array<string,string> $intervals
keywords
Possible keywords for months/weekdays.
private array $keywords
pcron
private int[] $pcron
Methods
__construct
Creates a new instance of the Cron class.
public __construct(int|string $expression): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$expression | int|string | The CRON expression to parse |
getNextOccurrence
Calculates the next time and date based on the supplied expression.
public getNextOccurrence(int $timestamp = null): ?int
If 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 = null): ?int
If 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(int[] $rtime, bool $next = true): ?int
Parameters:
Parameter | Type | Description |
---|---|---|
$rtime | int[] | reference-time |
$next | bool | true = nextOccurence, false = lastOccurence |
getTimestamp
Converts an unix-timestamp to an array.
private getTimestamp(int $timestamp = null): array
The 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, bool $next = true): mixed
If 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 | bool |
Return Value:
the next value or false if there isn't one
arrayReverse
Reverses all sub-arrays of our cron array.
private arrayReverse(array $cron): array
The 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): mixed
If 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): array
Parameters:
Parameter | Type | Description |
---|---|---|
$idx | int | |
$segment | string |
parseAtom
Analyses a single segment.
private parseAtom(string $atom): array
Parameters:
Parameter | Type | Description |
---|---|---|
$atom | string | The segment to parse |
Automatically generated on 2024-11-14