mikey-tralala/cron-expression

一个用于在PHP中使用cron表达式的库

1.0.0 2018-04-01 16:10 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:43:06 UTC


README

pipeline status coverage report

cron-expression 库可以解析类似 * * * * * 的cron表达式,并检查它是否到期。您还可以确定下一次或上一次运行日期。

此解析器可以处理正常cron语法接受的任何内容。只有一个例外,那就是命名构造(例如,mon-thu或jan-dec)。

安装

将依赖项添加到您的项目中

composer require mike-tralala/cron-expression

用法

<?php

require_once __DIR__ . '/vendor/autoload.php';

use MikeTralala\CronExpression\Expression\DueDateCalculator;
use MikeTralala\CronExpression\Expression\Expression;

// Works with predefined definitions
$expression = new Expression(Expression::WEEKLY);
$expression->isDue();

$expression = new Expression(Expression::MINUTELY);
$expression->isDue();

// Works with complex definitions
$expression = new Expression('10-40/10 0 */5 1 4,5');
$expression->isDue();

// Calculating next running date
$calculator = new DueDateCalculator();
$expression = new Expression('*/2 * * * *');

$dueDates = $calculator->getNextDueDates($expression, 5, \DateTime::createFromFormat('Y-m-d H:i', '2018-01-01 00:00'));

echo $expression . PHP_EOL;
foreach ($dueDates as $dueDate) {
    echo $dueDate->format('Y-m-d H:i') . PHP_EOL;
}

// Output:
 
// */2 * * * *
// 2018-01-01 00:00
// 2018-01-01 00:02
// 2018-01-01 00:04
// 2018-01-01 00:06
// 2018-01-01 00:08

CRON表达式

CRON表达式是一个表示特定命令执行计划的字符串。CRON计划的各个部分如下

*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    |
|    |    |    |    +----- day of week (0 - 6) (Sunday=0)
|    |    |    +---------- month (1 - 12)
|    |    +--------------- day of month (1 - 31)
|    +-------------------- hour (0 - 23)
+------------------------- min (0 - 59)

要求

  • PHP 5.5+
  • 需要PHPUnit来运行单元测试
  • 需要Composer来运行单元测试