phpcom-lab / hucron
v0.2.0
2021-05-29 04:25 UTC
Requires (Dev)
- phpunit/phpunit: ^7.5 || ^8.5
This package is auto-updated.
Last update: 2021-09-28 02:04:05 UTC
README
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脚本)定期运行。它通常用于自动化系统维护或管理——尽管其通用性质使其可用于诸如定期连接到互联网和下载电子邮件等事情。
相关
- https://github.com/dragonmantank/cron-expression 计算下一个或上一个运行日期并确定
- https://github.com/lorisleiva/cron-translator 使CRON表达式可读