首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在PHP中计算cron next-run-time

在PHP中计算cron next-run-time,可以使用以下代码:

代码语言:php
复制
<?php
function getNextRunTime($cron_expression) {
    $cron = preg_split("/[\s]+/", $cron_expression);
    if (count($cron) < 6) {
        return false;
    }
    $date = new DateTime();
    $date->setTimezone(new DateTimeZone('UTC'));
    $current_time = $date->format('Hi');
    $next_run_time = false;
    $next_run_time_in_seconds = PHP_INT_MAX;
    $current_time_in_seconds = strtotime($date->format('Y-m-d H:i:00'));
    $cron_parts = array(
        'minute' => $cron[0],
        'hour' => $cron[1],
        'day' => $cron[2],
        'month' => $cron[3],
        'weekday' => $cron[4],
    );
    for ($i = 0; $i < 60; $i++) {
        $date->modify('+1 minute');
        $next_time_in_seconds = strtotime($date->format('Y-m-d H:i:00'));
        if ($next_time_in_seconds > $current_time_in_seconds) {
            $found = true;
            foreach ($cron_parts as $cron_part_key => $cron_part_value) {
                if ($cron_part_key == 'minute' || $cron_part_key == 'hour') {
                    continue;
                }
                $value = $date->format('Y-m-d ') . $date->format('H:i:00');
                $value_in_seconds = strtotime($value);
                if (!checkCronExpression($cron_part_value, $value_in_seconds, $cron_part_key, $date)) {
                    $found = false;
                    break;
                }
            }
            if ($found) {
                $next_run_time = $date->format('Hi');
                $next_run_time_in_seconds = $next_time_in_seconds;
                break;
            }
        }
    }
    if ($next_run_time === false) {
        return false;
    }
    return $next_run_time;
}

function checkCronExpression($cron_part_value, $value_in_seconds, $cron_part_key, $date) {
    $cron_part_value = str_replace('*', '0', $cron_part_value);
    $cron_part_value = str_replace('?', '0', $cron_part_value);
    $values = explode(',', $cron_part_value);
    foreach ($values as $value) {
        $value = trim($value);
        $value = str_replace('/', '-', $value);
        $value = str_replace(' ', '', $value);
        $range = explode('-', $value);
        if (count($range) == 2) {
            $start = $range[0];
            $end = $range[1];
            if ($start > $end) {
                if ($value_in_seconds >= strtotime($date->format('Y-m-d ') . $start . ':00') && $value_in_seconds <= strtotime($date->format('Y-m-d ') . $end . ':59')) {
                    return true;
                }
            } else {
                if ($value_in_seconds >= strtotime($date->format('Y-m-d ') . $start . ':00') && $value_in_seconds <= strtotime($date->format('Y-m-d ') . $end . ':59')) {
                    return true;
                }
            }
        } else {
            $value = str_replace('-', ':', $value);
            if ($value_in_seconds == strtotime($date->format('Y-m-d ') . $value . ':00')) {
                return true;
            }
        }
    }
    return false;
}

这段代码可以计算cron表达式的下一次运行时间。它使用了PHP的DateTime类来处理日期和时间,并使用正则表达式来解析cron表达式。它还使用了一个名为checkCronExpression的辅助函数来检查cron表达式的每个部分是否匹配当前时间。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券