Parse and validate crontab expressions in PHP

Overview

PHP Cron Expression Parser

Build Status Code Coverage License Latest Stable Version Total Downloads

Standard (V7) compliant crontab expression parser/validator with support for time zones; see "man 5 crontab" for possible expressions.

Installation

Using composer, add a requirement for poliander/cron to your composer.json file:

composer require poliander/cron

Examples

Validate a certain crontab expression:

$expression = new \Cron\CronExpression('15,45 */2 * * *');
$isValid = $expression->isValid(); // returns true

Check whether a given point in time is matching a certain cron expression:

$expression = new \Cron\CronExpression('45 9 * * *');
$dt = new \DateTime('2014-05-18 09:45');
$isMatching = $expression->isMatching($dt); // returns true

Match an expression across different time zones:

$expression = new \Cron\CronExpression('45 9 * * *', new DateTimeZone('Europe/Berlin'));
$dt = new \DateTime('2014-05-18 08:45', new DateTimeZone('Europe/London'));
$isMatching = $expression->isMatching($dt); // returns true

Calculate next timestamp matching a Friday, the 13th:

$expression = new \Cron\CronExpression('* * 13 * fri');
$when = $expression->getNext();

Supported PHP versions

cron PHP
1.2.* 5.5 - 5.6
2.0.* 7.0
2.1.* 7.1
2.2.* 7.2
2.3.* 7.3 - 8.0

Changelog

version release notes
1.0.0 (2015-06-20) initial release
1.1.0 (2016-06-11) dropped PHP 5.4 support
1.2.0 (2016-12-11) added PHP 7.1 support
1.2.1 (2017-05-25) fixed #3
1.2.2 (2017-06-03) fixed #3, #4
2.0.0 (2017-11-30) dropped PHP 5.x, added PHP 7.2 support, added vendor namespace (closes #2)
2.1.0 (2018-12-08) dropped PHP 7.0, added PHP 7.3 support, updated PHPUnit dependency to 7.*
2.2.0 (2019-12-03) dropped PHP 7.1, added PHP 7.4 support, updated PHPUnit dependency to 8.*
2.3.0 (2020-12-29) dropped PHP 7.2, added PHP 8.0 support, updated PHPUnit dependency to 9.*
Comments
  • getNext() errors

    getNext() errors

    Thank you for the v3.0.1. It works better. But I think that I found 2 new errors.

    $tests = [
      [
        "expression" => '0 10 * * *',
        "timezone" => 'Europe/Paris',
        "now" => "2022-04-11T10:00:00+02:00", //1649664000
        "expected" => "2022-04-12T10:00:00+02:00" //1649750400
      ],
      [
        "expression" => '0 10 * * *',
        "timezone" => 'Europe/Paris',
        "now" => "2022-04-11T10:02:00+02:00", //1649664120
        "expected" => "2022-04-12T10:00:00+02:00" //1649750400
      ],
    ];
    
    foreach($tests as $test){
      $expr = new CronExpression($test['expression'], new DateTimeZone($timezone));
      $now = strtotime($test['now']);
      $test['getNext'] = $expr->getNext($now);
      $test['getNext_c'] = date('c', $test['getNext']);
      $test['result'] = ($test['getNext_c']==$test['expected']?"ok":"** ERROR **");
      print_r($test);
    }
    

    Output:

    Array
    (
        [expression] => 0 10 * * *
        [timezone] => Europe/Paris
        [now] => 2022-04-11T10:00:00+02:00
        [expected] => 2022-04-12T10:00:00+02:00
        [getNext] => 1649714400
        [getNext_c] => 2022-04-12T00:00:00+02:00
        [result] => ** ERROR **
    )
    Array
    (
        [expression] => 0 10 * * *
        [timezone] => Europe/Paris
        [now] => 2022-04-11T10:02:00+02:00
        [expected] => 2022-04-12T10:00:00+02:00
        [getNext] => 1649714400
        [getNext_c] => 2022-04-12T00:00:00+02:00
        [result] => ** ERROR **
    )
    
    bug 
    opened by ThomasPerraudin 5
  • Date slip with getNext() (again)

    Date slip with getNext() (again)

    Thank you for the v3.0.0. I don't know if it is linked to #9 and #11 but I still encounter a similar issue :

    $c = new \Cron('0 3 * * *', new \DateTimeZone('Europe/Paris'));
    echo $c->getNext();
    

    When run at 1649548801 //2022-04-10 02:00:01

    Expected:

    1649552400 //2022-04-10 03:00:00

    Actual:

    1649552400 //2022-04-10 03:00:00 OK


    When run at 1649549102 //2022-04-10 02:05:02

    Expected:

    1649552400 //2022-04-10 03:00:00

    Actual:

    1649638800 //2022-04-11 03:00:00 ERROR

    bug 
    opened by ThomasPerraudin 4
  • Date slip with getNext()

    Date slip with getNext()

    $c = new \Cron('0 3 * * *');
    echo $c->getNext();
    

    When run at 2022-03-25 01:55:01

    Expected:

    2022-03-25 03:00:00 ...

    Actual:

    2022-03-25 03:00:00 OK ...


    When run at 2022-03-25 02:00:01

    Expected:

    2022-03-25 03:00:00 ...

    Actual:

    2022-03-26 03:00:00 ERROR ...

    bug 
    opened by ThomasPerraudin 4
  • Next execution date skips a year if configured for January 1st at 00:00, but only from December 1st at 00:01

    Next execution date skips a year if configured for January 1st at 00:00, but only from December 1st at 00:01

    I've run into a strange problem while calculating the next execution date. I have a cron configured for January 1st at 00:00. It was working well before, saying that next execution would be on 2023-01-01 00:00. Today, however, it says 2024-01-01 00:00.

    Using the existing test for the class I was able to pinpoint the breaking point. The calculation for next year works well until December 1st at 00:00:59. Past this time, the class will calculate the next execution skipping one year.

    To reproduce, you can add this to getNextProvider on CronExpressionTest.php:

    ['0 0 1 1 *', 1669849200, 1672527600], // on 2022-12-01 00:00:00, next execution at 2023-01-01 00:00:00
    

    This one works well. 1669849200 is December 1st, 2022 at 00:00. 1672527600 is January 1st, 2023 at 00:00, as expected.

    ['0 0 1 1 *', 1669849260, 1672527600], // on 2022-12-01 00:01:00, next execution at 2024-01-01 00:00:00
    

    This one fails. 1669849260 is one minute later, December 1st, 2022 at 00:01. This results in 1704063600, which corresponds to January 1st, 2024 at 00:00.

    bug 
    opened by MikyKuroneko 3
  • Add a namespace

    Add a namespace

    The package is missing a namespace and could possibly use a more on-point name (like CronExpression or something). It's a small issue for a great tool, thanks! Saved me a bunch of time.

    enhancement 
    opened by renzo-s 2
  • Date slip with getNext()

    Date slip with getNext()

    Similar #9

    $c = new \Cron('0 3 * * *', new \DateTimeZone('Europe/Paris'));
    echo $c->getNext();
    

    When run at 1649202901 //2022-04-06 01:55:01

    Expected:

    1649206800 //2022-04-06 03:00:00

    Actual:

    1649206800 //2022-04-06 03:00:00 OK


    When run at 1649203201 //2022-04-06 02:00:01

    Expected:

    1649206800 //2022-04-06 03:00:00

    Actual:

    1649293200 //2022-04-07 03:00:00 ERROR

    bug duplicate 
    opened by ThomasPerraudin 1
  • Looping with getNext()

    Looping with getNext()

    $t = null;
    
    for ($i = 0; $i < 20; $i++) {
        $c = new \Cron('0 1 * * *');
        echo date('Y-m-d H:i:s', $t = $c->getNext($t)) . "<br />";
    }
    

    Expected:

    2017-06-04 01:00:00 2017-06-05 01:00:00 ...

    Actual:

    2017-06-04 01:00:00 2017-06-04 01:00:00 ...

    bug 
    opened by poliander 1
  • Suggest to use regular expression to check cron rule

    Suggest to use regular expression to check cron rule

    Hello, Sorry, I am not a native English speaker. I read the repo's source code. This is what I need. But this code can use regular expression to check cron rule more faster. This is my regular expression: ^((\d+|(\d+-\d+|\*)(/\d+)?)(,(\d+|(\d+-\d+|\*)(/\d+)?))*|[a-zA-Z]+)$.

    invalid 
    opened by taichunmin 1
Owner
René Pollesch
DevOps & Backend Development
René Pollesch
A package which provides a monthly calendar with days and events depending on given months and events.

A package which provides a monthly calendar with days and events depending on given months and events. This is where your description should go. Try a

MichaB 6 Nov 1, 2022
CRON for PHP: Calculate the next or previous run date and determine if a CRON expression is due

The PHP cron expression parser can parse a CRON expression, determine if it is due to run, calculate the next run date of the expression, and calculate the previous run date of the expression. You can calculate dates far into the future or past by skipping n number of matching dates.

Chris Tankersley 4.3k Jan 9, 2023
This library helps PHP users to convert and using Jalali DateTime format

Easy Jalali for PHP V1.0.0 Jalali calendar converter for Persian users in Iran, Afghanistan and other countries that use Jalali calendar. Very thanks

Majid J 3 Mar 20, 2022
Featured Calendar Maker v1.0 multingual extends the functionalities of the latest version of the FullCalendar (5.3.2), the most popular and completed JavaScript Calendar open source

Featured Calendar Maker v1.0 multingual extends the functionalities of the latest version of the FullCalendar (5.3.2), the most popular and completed JavaScript Calendar open source.

null 21 Oct 5, 2022
A simple PHP API extension for DateTime.

Carbon An international PHP extension for DateTime. http://carbon.nesbot.com <?php use Carbon\Carbon; printf("Right now is %s", Carbon::now()->toDat

Brian Nesbitt 16k Dec 30, 2022
The easy PHP Library for calculating holidays

Introduction Yasumi (Japanese for 'Holiday'「休み」) is the easy PHP library that helps you retrieve the dates and names of holidays and other special cel

AzuyaLabs 926 Dec 29, 2022
The missing PHP 5.3+ calendar management library.

CalendR CalendR is an Object Oriented Calendar management library on top of PHP5.3+ Date objects. You can use it to deal with all your needs about cal

Yohan Giarelli 462 Dec 30, 2022
Date Manager PHP Class

Date Manager PHP Class Date Manager Version 1.0.0 PHP class for date management, for example converting solar date to gregorian date and vice versa. C

Alireza Tolouei 2 Dec 21, 2021
iCal-creator for PHP - This package offers an abstraction layer for creating iCalendars files

This package offers an abstraction layer for creating iCalendars files. By using this PHP package, you can create *.ics files without the knowledge of the underling format. The output itself will follow RFC 5545 as good as possible.

Markus Poerschke 1k Dec 23, 2022
YCOM Impersonate. Login as selected YCOM user 🧙‍♂️in frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Friends Of REDAXO 17 Sep 12, 2022
Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js

Support I am a dad now for the last 1,5 years and that clearly shows in being on time with merging PRs or pushing this package further. Time is the bi

Tino Ehrich 944 Dec 21, 2022
Manage all your cron jobs without modifying crontab. Handles locking, logging, error emails, and more.

Jobby, a PHP cron job manager Install the master jobby cron job, and it will manage all your offline tasks. Add jobs without modifying crontab. Jobby

null 1k Dec 25, 2022
Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don't need to touch your crontab when deploying.

Dispatcher Dispatcher allows you to schedule your artisan commands within your Laravel project, eliminating the need to touch the crontab when deployi

Indatus 1.1k Jan 5, 2023
Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don't need to touch your crontab when deploying.

Dispatcher Dispatcher allows you to schedule your artisan commands within your Laravel project, eliminating the need to touch the crontab when deployi

Indatus 1.1k Dec 21, 2022
High performance view templating API for PHP applications using tags & expressions inspired by Java JSTL and C compiler

View Language API Table of contents: About Expressions Tags Configuration Compilation Installation Unit Tests Examples Reference Guide About This API

Lucian Gabriel Popescu 0 Jan 9, 2022
Enable method chaining or fluent expressions for any value and method.

PHP Pipe Operator A (hopefully) temporary solution to implement the pipe operator in PHP. Table of contents Requirements How to install How to use The

Sebastiaan Luca 268 Dec 26, 2022
PHP Regular expressions made easy

PHPVerbalExpressions ported from VerbalExpressions VerbalExpressions is a PHP library that helps to construct hard regular expressions. Installation T

null 2.4k Jan 3, 2023
Fluent regular expressions in PHP

FLUX (Fluent Regex) 0.5.2 by Selvin Ortiz Description Fluent Regular Expressions in PHP inspired by and largely based on VerbalExpressions:JS by Jesse

Selvin Ortiz 341 Nov 20, 2022
PHP Regular expressions made easy

PHPVerbalExpressions ported from VerbalExpressions VerbalExpressions is a PHP library that helps to construct hard regular expressions. Installation T

null 2.4k Dec 18, 2022
🦉 human-readable regular expressions for PHP

RegExpBuilder integrates regular expressions into the programming language, thereby making them easy to read and maintain. Regular Expressions are created by using chained methods and variables such as arrays or strings.

Max Girkens 907 Dec 30, 2022