Install dragonmantank/cron-expression via Composer
masterAdd the dependency to your project using Composer to enable CRON expression parsing in your PHP application.
composer require dragonmantank/cron-expressionrepository·master·Indexed 26 days ago
https://github.com/dragonmantank/cron-expressionA PHP library for parsing CRON expressions to determine if a schedule is due and calculate next or previous run dates. It supports standard CRON syntax, macros (e.g., @daily, @hourly), and advanced scheduling logic such as nearest weekdays (W), last day of month (L), and nth weekdays (#). The Cron\CronExpression class provides methods for validating expressions, managing custom aliases, and retrieving multiple scheduled run dates.
Add the dependency to your project using Composer to enable CRON expression parsing in your PHP application.
composer require dragonmantank/cron-expressionThe Cron\CronExpression class allows you to parse CRON expressions (including macros like @daily), check if a schedule is currently due, and calculate the next or previous run dates. You can also calculate future dates by skipping iterations or calculate dates relative to a specific timestamp.
<?php
require_once '/vendor/autoload.php';
// Works with predefined scheduling definitions
$cron = new Cron\CronExpression('@daily');
$cron->isDue();
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');
echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s');
// Works with complex expressions
$cron = new Cron\CronExpression('3-59/15 6-12 */15 1 2-5');
echo $cron->getNextRunDate()->format('Y-m-d H:i:s');
// Calculate a run date two iterations into the future
$cron = new Cron\CronExpression('@daily');
echo $cron->getNextRunDate(null, 2)->format('Y-m-d H:i:s');
// Calculate a run date relative to a specific time
$cron = new Cron\CronExpression('@monthly');
echo $cron->getNextRunDate('2010-01-12 00:00:00')->format('Y-m-d H:i:s');The parser supports specialized characters for complex scheduling requirements:
W): Finds the nearest weekday (Mon-Fri) to the given day. e.g., * * 15W * *. If the 15th is Saturday, it triggers on Friday the 14th.L): e.g., * * L * *.LW): e.g., * * LW * *.#): Finds the nth occurrence of a weekday in a month. e.g., * * * * 7#4 targets the 4th Sunday.L): Finds the last occurrence of a specific weekday in a month. e.g., * * * * 7L targets the last Sunday.To use this library, ensure your environment meets the following requirements:
A CRON expression consists of five parts: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-7, where Sunday is 0 or 7).
* (matches always). ? is an alias for * in day of week and day of month., (e.g., 15,30 * * * * for minutes 15 and 30).- (e.g., 1-9 * * * * for minutes 1 through 9)./ (e.g., */5 * * * * for every 5th minute).0-14,30-44 * * * *.@yearly or @annually: 0 0 1 1 *@monthly: 0 0 1 * *@weekly: 0 0 * * 0@daily or @midnight: 0 0 * * *@hourly: 0 * * * *isValidExpression() to check if a string is a valid CRON expression without instantiating the class.The Cron\ CronExpression class is the primary interface for parsing CRON expressions and calculating scheduled run dates. It supports standard CRON parts (minute, hour, day of month, month, day of week) and an optional year. You can instantiate it with a CRON string or use built-in aliases like @daily or @hourly.
unregisterAlias() to remove a previously registered custom alias. Note that you cannot unregister built-in aliases (like @daily).The FieldFactory class implements a flyweight factory pattern to provide instances of FieldInterface for specific cron expression positions. Use the getField(int $position) method to retrieve a field object. The factory caches instantiated fields to optimize performance.
Supported positions correspond to the constants defined in CronExpression:
CronExpression::MINUTECronExpression::HOURCronExpression::DAYCronExpression::MONTHCronExpression::WEEKDAYThrows an InvalidArgumentException if the provided position is not a valid cron field position.
Use getMultipleRunDates() to retrieve an array of upcoming (or past) scheduled dates.
Parameters:
$total (int): The number of dates to return.$currentTime (string|\DateTimeInterface|null): The starting point.$invert (bool): If true, retrieves previous dates instead of future dates.$allowCurrentDate (bool): If true, includes the $currentTime if it matches.$timeZone (string|null): The timezone to use.Use getPreviousRunDate() to find the most recent time a CRON expression triggered relative to a specific time.
Parameters:
$currentTime (string|\DateTimeInterface): The starting point for calculation (defaults to 'now').$nth (int): Number of matches to skip. 0 returns the immediate previous match.$allowCurrentDate (bool): If true, returns the $currentTime if it matches the expression.$timeZone (string|null): A specific timezone to use for the calculation.@ and contain alphanumeric characters or underscores. This allows you to use human-readable names for complex expressions.