CronExpression::getRunDate()protectedWC 1.0

Get the next or previous run date of the expression relative to a date

Method of the class: CronExpression{}

No Hooks.

Return

DateTime.

Usage

// protected - for code of main (parent) or child class
$result = $this->getRunDate( $currentTime, $nth, $invert, $allowCurrentDate );
$currentTime(string|DateTime)
(optional) Relative calculation date
Default: null
$nth(int)
(optional) Number of matches to skip before returning
$invert(true|false)
(optional) Set to TRUE to go backwards in time
Default: false
$allowCurrentDate(true|false)
(optional) Set to TRUE to return the
php current date if it matches the cron expression
Default: false

CronExpression::getRunDate() code WC 8.7.0

protected function getRunDate($currentTime = null, $nth = 0, $invert = false, $allowCurrentDate = false)
{
    if ($currentTime instanceof DateTime) {
        $currentDate = $currentTime;
    } else {
        $currentDate = new DateTime($currentTime ? $currentTime : 'now');
        $currentDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
    }

    $currentDate->setTime($currentDate->format('H'), $currentDate->format('i'), 0);
    $nextRun = clone $currentDate;
    $nth = (int) $nth;

    // Set a hard limit to bail on an impossible date
    for ($i = 0; $i < 1000; $i++) {

        foreach (self::$order as $position) {
            $part = $this->getExpression($position);
            if (null === $part) {
                continue;
            }

            $satisfied = false;
            // Get the field object used to validate this part
            $field = $this->fieldFactory->getField($position);
            // Check if this is singular or a list
            if (strpos($part, ',') === false) {
                $satisfied = $field->isSatisfiedBy($nextRun, $part);
            } else {
                foreach (array_map('trim', explode(',', $part)) as $listPart) {
                    if ($field->isSatisfiedBy($nextRun, $listPart)) {
                        $satisfied = true;
                        break;
                    }
                }
            }

            // If the field is not satisfied, then start over
            if (!$satisfied) {
                $field->increment($nextRun, $invert);
                continue 2;
            }
        }

        // Skip this match if needed
        if ((!$allowCurrentDate && $nextRun == $currentDate) || --$nth > -1) {
            $this->fieldFactory->getField(0)->increment($nextRun, $invert);
            continue;
        }

        return $nextRun;
    }

    // @codeCoverageIgnoreStart
    throw new RuntimeException('Impossible CRON expression');
    // @codeCoverageIgnoreEnd
}