Libraries / cron
Overview
Zero-dependency 5-field cron expression parser, validator, matcher, human-readable describer, and next-match finder for HolyPHP.
bundled — works offline
zero dependencies
schedule
timer
Quick start
import "cron";
$expr = cron_parse("*/15 9-17 * * MON-FRI");
println($expr->describe());
// Every 15 minutes, past hour 9, 10, 11, 12, 13, 14, 15, 16, 17, on Monday, Tuesday, Wednesday, Thursday, Friday
// Check if 09:30 on Wednesday, March 12 matches
$ok = $expr->matches(30, 9, 12, 3, 3); // true
// Find next occurrence after 2025-03-12 09:30
$next = $expr->nextMatch(2025, 3, 12, 9, 30);
// ["year" => 2025, "month" => 3, "day" => 12, "hour" => 9, "minute" => 45, "weekday" => 3]
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages ):
hphp install cron
This adds the library to hphp.yaml, installs it into .hphp/packages/cron/ and pins the exact version in hphp.lock:
require:
cron: "^1.0"
Use it
import "cron";
cron is bundled with the compiler, so import "cron"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install cron # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install cron
Documentation
Supported Syntax
Standard 5-field format: minute hour day-of-month month day-of-week
Field Allowed Values Special Names
Minute 0-59—
Hour 0-23—
Day of Month 1-31—
Month 1-12JAN–DEC (case-insensitive)
Day of Week 0-7 (0 and 7 are Sunday)SUN–SAT (case-insensitive)
Supported operators:
* — any value
*/n — every n units
a-b — inclusive range
a-b/n — range with step n
a/n — starting at a with step n up to field max
a,b,c — comma-separated list of values/ranges/steps
Aliases: @yearly, @annually, @monthly, @weekly, @daily, @midnight, @hourly
API Reference
Class CronExpr
new CronExpr(string $expr) — Parses and validates a 5-field cron expression (or alias).
$expr->toString(): string — Returns the normalized 5-field expression string.
$expr->matches(int $minute, int $hour, int $day, int $month, int $weekday): bool — Returns true if the given time matches the expression. Uses standard cron OR semantics when both day-of-month and day-of-week are restricted.
$expr->describe(): string — Returns a human-readable description of the schedule.
$expr->nextMatch(int $year, int $month, int $day, int $hour, int $minute): array — Returns the next matching date/time strictly after the given timestamp as an associative array with keys year, month, day, hour, minute, weekday.
Properties on CronExpr:
string $expr — Normalized expression string
array $minutes, $hours, $days, $months, $weekdays — Sorted unique arrays of matching integers
Functions
cron_parse(string $expr): CronExpr — Parses a cron expression and returns a CronExpr instance.
cron_validate(string $expr): bool — Returns true if the expression is valid, false otherwise.
cron_matches(string $expr, int $minute, int $hour, int $day, int $month, int $weekday): bool — Convenience wrapper for parsing and matching.
cron_describe(string $expr): string — Convenience wrapper for parsing and describing.
cron_next(string $expr, int $year, int $month, int $day, int $hour, int $minute): array — Convenience wrapper for parsing and finding the next match.
Error Handling
All invalid expressions and out-of-range arguments throw Exception with clear, field-level messages prefixed with cron::
Wrong field count: cron: expected 5 fields (minute hour day month weekday), got 3 in '* * *'
Out-of-range value: cron: minute value 60 out of range (0-59)
Invalid step: cron: step must be greater than 0 in hour field '*/0'
Reversed range: cron: range start 10 is greater than end 5 in day field '10-5'
Invalid token: cron: invalid month value 'FOO' (expected integer between 1 and 12)
Impossible schedule: cron: no matching date found within 4 years for expression '0 0 31 2 *'
Examples
01 Quick start
Bundled with the compiler — import "cron"; works offline in any file, or add it to a project with hphp install cron.
import "cron";
$expr = cron_parse("*/15 9-17 * * MON-FRI");
println($expr->describe());
// Every 15 minutes, past hour 9, 10, 11, 12, 13, 14, 15, 16, 17, on Monday, Tuesday, Wednesday, Thursday, Friday
// Check if 09:30 on Wednesday, March 12 matches
$ok = $expr->matches(30, 9, 12, 3, 3); // true
// Find next occurrence after 2025-03-12 09:30
$next = $expr->nextMatch(2025, 3, 12, 9, 30);
// ["year" => 2025, "month" => 3, "day" => 12, "hour" => 9, "minute" => 45, "weekday" => 3]
Try it hphp install cron
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_cron.hphp.