Files
kiri-core/p.php
T

133 lines
2.7 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2021-08-25 19:11:15 +08:00
date_default_timezone_set('Asia/Shanghai');
class Crontab
{
public bool $isLoop = false;
const LOOP_TYPE_YEAR = 0;
const LOOP_TYPE_MONTH = 1;
const LOOP_TYPE_DAY = 2;
const LOOP_TYPE_HOUR = 3;
const LOOP_TYPE_MINUTE = 4;
const LOOP_TYPE_SECOND = 5;
public string $crontab = '2021 * * * */2 */5';
public int $loopType = Crontab::LOOP_TYPE_MINUTE;
public int $loopTime = 2;
2021-08-26 15:03:19 +08:00
private int|string $month = '*';
2021-08-25 19:11:15 +08:00
2021-08-26 15:03:19 +08:00
private int|string $day = '*';
2021-08-25 19:11:15 +08:00
2021-08-26 15:03:19 +08:00
private int|string $hour = '*';
2021-08-25 19:11:15 +08:00
2021-08-26 15:03:19 +08:00
private int|string $minute = '*/2';
2021-08-25 19:11:15 +08:00
2021-08-26 15:03:19 +08:00
private int|string $second = '1-30';
2021-08-25 19:11:15 +08:00
2021-08-26 15:03:19 +08:00
private int|string $week = '*';
2021-08-25 19:11:15 +08:00
public function __construct()
{
}
/**
* @return bool
*/
public function canExecute(): bool
{
$match = $this->next();
if (str_contains($match, '^')) {
return false;
}
return true;
}
public function next(): string
{
2021-08-26 15:03:19 +08:00
$time = time();
return sprintf('%s-%s-%s %s:%s:%s %s',
date('Y'),
$this->format($time, $this->month, 'm', 'month'),
$this->format($time, $this->day, 'd', 'day'),
$this->format($time, $this->hour, 'H', 'hour'),
$this->format($time, $this->minute, 'i', 'minute'),
$this->format($time, $this->second, 's', 'second'),
$this->format($time, $this->week, 'N'),
2021-08-25 19:11:15 +08:00
);
}
/**
2021-08-26 15:03:19 +08:00
* @param int $startTime
2021-08-25 19:11:15 +08:00
* @param string $text
* @param string $match
2021-08-26 15:03:19 +08:00
* @param string|null $format
2021-08-25 19:11:15 +08:00
* @return string
*/
2021-08-26 15:03:19 +08:00
private function format(int &$startTime, string $text, string $match, ?string $format = null): string
2021-08-25 19:11:15 +08:00
{
$time = date($match);
2021-08-26 15:03:19 +08:00
if ($text == '*' || $text == '*/1') {
2021-08-25 19:11:15 +08:00
return $time;
}
if (str_contains($text, ',')) {
$explode = explode(',', $text);
sort($explode, SORT_NUMERIC);
if (in_array($time, $explode)) {
return $explode[array_search($time, $explode) + 1];
}
return '^';
}
if (str_contains($text, '-')) {
$explode = explode('-', $text);
if ($time >= $explode[0] && $time <= $explode[1]) {
2021-08-26 15:03:19 +08:00
return intval($time);
2021-08-25 19:11:15 +08:00
}
return '^';
}
if (str_contains($text, '/')) {
$explode = explode('/', $text);
2021-08-26 15:03:19 +08:00
if ($time % $explode[1] !== 0) {
2021-08-25 19:11:15 +08:00
return '^';
}
if ($explode[0] != '*') {
return $explode[0] == $text ? $time : '^';
}
return $time;
}
return $time == $text ? $time : '^';
}
}
2021-08-26 15:03:19 +08:00
//$date = date('Y-m-d H:i:s');
//var_dump(date('Y-m-d H:i:s', strtotime('+10 month', strtotime($date))));
//var_dump(date('Y-m-d H:i:s', strtotime('+10 day', strtotime($date))));
//var_dump(date('Y-m-d H:i:s', strtotime('+10 hour', strtotime($date))));
//var_dump(date('Y-m-d H:i:s', strtotime('+10 minute', strtotime($date))));
//var_dump(date('Y-m-d H:i:s', strtotime('+10 second', strtotime($date))));
//var_dump(date('Y-m-d H:i:s', strtotime('+10 week', strtotime($date))));
2021-08-25 19:11:15 +08:00
$c = new Crontab();
while (true) {
var_dump($c->next());
sleep(1);
}