-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crontab.php
80 lines (73 loc) · 2.27 KB
/
Crontab.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Created by PhpStorm.
* User: yhm
* Date: 19-9-29
* Time: 下午4:51
*/
/**
* Class Crontab
* description: 使用 php 实现类似 linux crontab 的定时任务功能,支持秒级定时
* author: [email protected]
*/
class Crontab {
/**
* 判断某个时间点是否在 cron 规则之内
* @param string $cron 规则字符串,示例: "秒 分 时 日 月 周",规则类似 linux crontab 写法
* @param int $time
* @return bool
*/
public static function parse($cron, $time) {
$cronArray = self::getCronArray($cron);
$now = explode(' ', date('s i G j n w', $time));
foreach ($now as $key => $piece) {
if (!in_array($piece, $cronArray[$key])) {
return false;
}
}
return true;
}
/**
* @param $cron
* @return array
*/
private static function getCronArray($cron) {
$cronArray = explode(' ', $cron);
$timeArray = [];
$dimensions = [
[0, 59], //seconds
[0, 59], //Minutes
[0, 23], //Hours
[1, 31], //Days
[1, 12], //Months
[0, 6] //Weekdays
];
foreach ($cronArray as $key => $item) {
list($repeat, $every) = explode('/', $item, 2) + [false, 1];
if ($repeat === '*') {
$timeArray[$key] = range($dimensions[$key][0], $dimensions[$key][1]);
} else {
// 处理逗号拼接的命令
$tmpRaw = explode(',', $item);
foreach ($tmpRaw as $tmp) {
// 处理10-20这样范围的命令
$tmp = explode('-', $tmp, 2);
if (count($tmp) == 2) {
$timeArray[$key] = array_merge($timeArray[$key], range($tmp[0], $tmp[1]));
} else {
$timeArray[$key][] = $tmp[0];
}
}
}
// 判断*/10 这种类型的
if ($every > 1) {
foreach ($timeArray[$key] as $k => $v) {
if ($v % $every != 0) {
unset($timeArray[$key][$k]);
}
}
}
}
return $timeArray;
}
}