PHP Task Scheduler: cron-expression Library for Flexible Scheduling

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PHP Task Scheduler: cron-expression Library



When developing web applications in PHP, there is often a need to perform certain actions on a schedule: send notifications, clear cache, generate reports, or synchronize data. The built-in cron in Linux is a powerful tool, but its configuration requires server access and is not always convenient for dynamic scheduling within PHP code. The cron-expression library solves this problem — it allows you to parse, validate, and calculate task execution times based on CRON expressions directly in PHP.



In this article, we will cover how to install and use the cron-expression library, write a simple task scheduler, and look at examples with real-world scenarios.



What is cron-expression?



cron-expression is a PHP library for working with CRON expressions. A CRON expression is a string consisting of five fields separated by spaces:



* * * * *| | | | || | | | +---- day of week (0–7) (Sunday = 0 or 7)| | | +------ month (1–12)| | +-------- day of month (1–31)| +---------- hour (0–23)+------------ minute (0–59)


For example, the expression 30 4 * * 1-5 means: "every weekday at 4:30 AM". The library allows you to:



  • Check if the current time matches a given expression.
  • Get the next execution date of a task.
  • Calculate the previous execution date.
  • Work with ranges, steps, and lists of values.


Installation



Installation is done via Composer. Open a terminal in your project root and run:



composer require mtdowling/cron-expression


After installation, include the Composer autoloader in your PHP script:



require 'vendor/autoload.php';


Main Features and Examples



1. Checking the Current Time



Suppose we want to run a task every 15 minutes. The CRON expression is: */15 * * * *. Let's create an object and check if the task should run right now:



<?phpuse Cron\\CronExpression;

$expression = CronExpression::factory('*/15 * * * *');if ($expression->isDue()) { echo "The task should be executed now!";} else { echo "The time has not come yet.";}?>


The isDue() method checks if the current time matches the expression. By default, the server's timezone is used.



2. Getting the Next Execution Date



Sometimes you need to know in advance when a task will run:



<?phpuse Cron\\CronExpression;

$expression = CronExpression::factory('0 6 * * 1-5'); // every weekday at 6:00 AM$nextRun = $expression->getNextRunDate();echo "Next run: " . $nextRun->format('Y-m-d H:i:s');?>


Result: Next run: 2025-03-10 06:00:00 (if today is Friday, then next Monday).



3. Previous Execution Date



The getPreviousRunDate() method returns the last time the condition was met:



<?php$prevRun = $expression->getPreviousRunDate();echo "Previous run: " . $prevRun->format('Y-m-d H:i:s');?>


4. Working with Timezones



By default, the server's timezone is used, but you can set your own:



<?php$expression = CronExpression::factory('0 9 * * *');$expression->setTimezone(new DateTimeZone('Europe/Moscow'));if ($expression->isDue()) {    echo "It is 9:00 AM in Moscow — time to act!";}?>


Creating a Simple Task Scheduler



Now let's put it all together and write a small class that will execute tasks on a schedule. Suppose we have a list of tasks with CRON expressions and callbacks.



<?phpclass                    

Recommendations