You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.6 KiB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. PHP Cron Expression Parser
  2. ==========================
  3. [![Latest Stable Version](https://poser.pugx.org/mtdowling/cron-expression/v/stable.png)](https://packagist.org/packages/mtdowling/cron-expression) [![Total Downloads](https://poser.pugx.org/mtdowling/cron-expression/downloads.png)](https://packagist.org/packages/mtdowling/cron-expression) [![Build Status](https://secure.travis-ci.org/mtdowling/cron-expression.png)](http://travis-ci.org/mtdowling/cron-expression)
  4. The PHP cron expression parser can parse a CRON expression, determine if it is
  5. due to run, calculate the next run date of the expression, and calculate the previous
  6. run date of the expression. You can calculate dates far into the future or past by
  7. skipping n number of matching dates.
  8. The parser can handle increments of ranges (e.g. */12, 2-59/3), intervals (e.g. 0-9),
  9. lists (e.g. 1,2,3), W to find the nearest weekday for a given day of the month, L to
  10. find the last day of the month, L to find the last given weekday of a month, and hash
  11. (#) to find the nth weekday of a given month.
  12. Installing
  13. ==========
  14. Add the dependency to your project:
  15. ```bash
  16. composer require mtdowling/cron-expression
  17. ```
  18. Usage
  19. =====
  20. ```php
  21. <?php
  22. require_once '/vendor/autoload.php';
  23. // Works with predefined scheduling definitions
  24. $cron = Cron\CronExpression::factory('@daily');
  25. $cron->isDue();
  26. echo $cron->getNextRunDate()->format('Y-m-d H:i:s');
  27. echo $cron->getPreviousRunDate()->format('Y-m-d H:i:s');
  28. // Works with complex expressions
  29. $cron = Cron\CronExpression::factory('3-59/15 2,6-12 */15 1 2-5');
  30. echo $cron->getNextRunDate()->format('Y-m-d H:i:s');
  31. // Calculate a run date two iterations into the future
  32. $cron = Cron\CronExpression::factory('@daily');
  33. echo $cron->getNextRunDate(null, 2)->format('Y-m-d H:i:s');
  34. // Calculate a run date relative to a specific time
  35. $cron = Cron\CronExpression::factory('@monthly');
  36. echo $cron->getNextRunDate('2010-01-12 00:00:00')->format('Y-m-d H:i:s');
  37. ```
  38. CRON Expressions
  39. ================
  40. A CRON expression is a string representing the schedule for a particular command to execute. The parts of a CRON schedule are as follows:
  41. * * * * * *
  42. - - - - - -
  43. | | | | | |
  44. | | | | | + year [optional]
  45. | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
  46. | | | +---------- month (1 - 12)
  47. | | +--------------- day of month (1 - 31)
  48. | +-------------------- hour (0 - 23)
  49. +------------------------- min (0 - 59)
  50. Requirements
  51. ============
  52. - PHP 5.3+
  53. - PHPUnit is required to run the unit tests
  54. - Composer is required to run the unit tests