cron

5-field cron expression parser, matcher, human describer and next-match finder.

v1.0.0Math, time & IDsbundledimport "cron";
Overview Installation Documentation Examples

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
Packagecron
Version1.0.0 (follows the compiler)
Entry filecron.hphp
Sourcepackages/lib/cron ↗

Documentation

Supported Syntax

Standard 5-field format: minute hour day-of-month month day-of-week

FieldAllowed ValuesSpecial Names
Minute0-59—
Hour0-23—
Day of Month1-31—
Month1-12JAN–DEC (case-insensitive)
Day of Week0-7 (0 and 7 are Sunday)SUN–SAT (case-insensitive)

Supported operators:

API Reference

Class CronExpr

Properties on CronExpr:

Functions

Error Handling

All invalid expressions and out-of-range arguments throw Exception with clear, field-level messages prefixed with cron::

Examples

01Quick 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.