Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jupitern committed Dec 5, 2024
1 parent a9089bc commit 59beeba
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,26 @@ public function getNextSchedules(string $fromDateStr = 'now', int $limit = 5): a
$dates = [];

foreach ($this->oneTimeEvents as $schedule) {
$dt = new \DateTime($fromDateStr);
$dt->modify($schedule);
if ($schedule == 'now') $schedule = '+1 second';
$d = new \DateTime($fromDateStr);
$d->modify($schedule);

if ($this->isInTimeFrame($dt, $fromDateStr)) {
$dates[$dt->format('YmdHis')] = $dt;
if ($this->isInTimeFrame($d, $fromDateStr)) {
$dates[$d->format('YmdHis')] = clone $d;
}
}

foreach ($this->schedules as $schedule) {
$d = new \DateTime($fromDateStr);

for ($i=0, $maxRecursion = 100 * $limit; $i < $limit && $maxRecursion > 0; ++$i, --$maxRecursion) {

if ($this->isDateRelative($schedule)) {
$d->modify($schedule);

if ($this->startTime instanceof \DateTime && $d < $this->startTime->modify($d->format('Y-m-d'))) {
$d->modify($this->startTime->format('H:i:s'));
} elseif ($this->endTime instanceof \DateTime && $d > $this->endTime->modify($d->format('Y-m-d'))) {
$d->modify('next day')->modify($this->startTime->format('H:i:s'));
} else {
$d->modify($schedule);
}

if (!array_key_exists($d->format('YmdHis'), $dates)) {
Expand All @@ -124,6 +124,7 @@ public function getNextSchedules(string $fromDateStr = 'now', int $limit = 5): a
}

$this->orderDates($dates);

return array_slice($dates, 0, $limit);
}

Expand All @@ -135,7 +136,7 @@ public function getNextSchedules(string $fromDateStr = 'now', int $limit = 5): a
private function orderDates(array &$dates): void
{
uasort($dates, function($a, $b) {
return strtotime($a->format('Y-m-d H:i:s')) > strtotime($b->format('Y-m-d H:i:s')) ? 1 : -1;
return strtotime($a->format('Y-m-d H:i:s')) - strtotime($b->format('Y-m-d H:i:s'));
});
}

Expand Down

0 comments on commit 59beeba

Please sign in to comment.