startTime = time(); } /** * @return bool */ public function canExecute(): bool { $match = $this->next(); if (str_contains($match, '^')) { return false; } return true; } public function next(): string { if ($this->loopType == Crontab::LOOP_TYPE_YEAR) $this->year = '*/' . $this->loopTime; if ($this->loopType == Crontab::LOOP_TYPE_MONTH) $this->month = '*/' . $this->loopTime; if ($this->loopType == Crontab::LOOP_TYPE_DAY) $this->day = '*/' . $this->loopTime; if ($this->loopType == Crontab::LOOP_TYPE_HOUR) $this->hour = '*/' . $this->loopTime; if ($this->loopType == Crontab::LOOP_TYPE_MINUTE) $this->minute = '*/' . $this->loopTime; if ($this->loopType == Crontab::LOOP_TYPE_SECOND) $this->second = '*/' . $this->loopTime; return sprintf('%s-%s-%s %s:%s:%s', $this->format($this->year, 'Y'), $this->format($this->month, 'm'), $this->format($this->day, 'd'), $this->format($this->hour, 'H'), $this->format($this->minute, 'i'), $this->format($this->second, 's') ); } /** * @param string $text * @param string $match * @return string */ private function format(string $text, string $match): string { $time = date($match); if ($text == '*') { 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]) { return intval($time) + 1; } return '^'; } if (str_contains($text, '/')) { $explode = explode('/', $text); if ($time % $this->loopTime !== 0) { return '^'; } if ($explode[0] != '*') { return $explode[0] == $text ? $time : '^'; } return $time; } return $time == $text ? $time : '^'; } } $c = new Crontab(); while (true) { var_dump($c->next()); sleep(1); }