phpcom-lab/hucron

此包已被弃用且不再维护。作者建议使用phppkg/hucron包。

将crontabs表示为可读的短语

v0.2.0 2021-05-29 04:25 UTC

README

GitHub tag (latest SemVer) Github Actions Status

HuCron将关于时间的可读字符串转换为有效的crontabs。

phpcom-lab/hucron是从https://github.com/ajbdev/cronlingo派生的。

注意:仓库已移动到https://github.com/phppkg/hucron

安装

composer install phpcom-lab/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”)识别字符串的部分,并据此推断时间含义,将其转换为crontab的一部分。它对语句的顺序并不特别关心。以下是一个简短的列表,列出了它将识别并解析为crontab的内容:

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

什么是crontab?

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

它看起来像这样

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

来自维基百科

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

相关

许可证

MIT