phppkg/hucron

将cron任务以可读性强的短语表达

1.0.0 2021-09-28 02:00 UTC

This package is auto-updated.

Last update: 2024-09-09 21:18:47 UTC


README

GitHub tag (latest SemVer) Github Actions Status Php Version License

HuCron 将关于时间的人读性字符串转换为有效的cron任务。

phppkg/hucron 来自于 https://github.com/ajbdev/cronlingo

安装

composer install phppkg/hucron

用法

有一些解析人类语句的示例。

快捷语句

use HuCron\HuCron;

echo HuCron::fromStatement('@hourly'); // "0 * * * *"

echo HuCron::fromStatement('@daliy'); // "0 0 * * *"

echo HuCron::fromStatement('@weekly'); // "0 0 * * 0"

echo HuCron::fromStatement('@monthly'); // "0 0 1 * *"

// echo HuCron::fromStatement('@yearly');
echo HuCron::fromStatement('@annually'); // "0 0 1 1 *"

自定义语句

use HuCron\HuCron;

echo HuCron::fromStatement('Every day at midnight');
// "0 0 * * *"

echo HuCron::fromStatement('Every 15 minutes at midnight on the weekend');
// "*/15 0 * * 0,6"

echo HuCron::fromStatement('Every other minute in August at noon on a weekday');
// "*/2 12 * 8 1,2,3,4,5"

echo HuCron::fromStatement('The 1st day in April at midnight');
// "0 0 1 4 *"

echo HuCron::fromStatement('Every day on the weekday at 2:25pm');
// "25 14 * * 1,2,3,4,5"

语法

HuCron 使用特定的与时间相关的关键词(如“on, to, at”)来识别字符串的各个部分,并据此推断时间含义并将其转换为cron表的一部分。它并不关心这些语句的顺序。以下是一些它将识别并解析为cron表的内容的简要列表:

  • 周期(daily, weekly, monthly
  • 确切时间(9:30 PM, 8a, 3p
  • 昼夜(AM/PM/A/P
  • 间隔(1st, second, other, ninth, etc
  • 特定字段(second, minute, hour, day, month
  • 星期几(sunday, monday 等)
  • 12点(noon, midnight
  • 列表(例如 5 to 12 minutes
  • 月份名称(january, february 等)
  • 周末/工作日

什么是cron表?

cron表是一个定义重复时间周期的表达式。

它看起来像这样

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

来自 Wikipedia

软件实用程序Cron是类Unix计算机操作系统中基于时间的工作调度程序。设置和维护软件环境的人使用cron来安排作业(命令或shell脚本)在固定的时间、日期或间隔定期运行。它通常用于自动化系统维护或管理——尽管其通用性质使其对于定期连接到互联网和下载电子邮件等任务也很有用。

相关

许可证

MIT