且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP - 找到下一个未来的定期日期?

更新时间:2022-02-07 09:20:25

我知道你自己回答了这个问题,但另一个选择是使用模数)减去开始日期,以计算您的下一个日期。这是一个简单的脚本:

I know you answered this yourself, however another option is to just use the modulus (remainder after devision) of the current date minus the start date to compute your next date. Here is a quick script to do just that :

<?php
function nextDate($start_date,$interval_days,$output_format){
    $start = strtotime($start_date);
    $end = strtotime(date('Y-m-d'));
    $days_ago = ($end - $start) / 24 / 60 / 60;
    if($days_ago < 0)return date($output_format,$start);
    $remainder_days = $days_ago % $interval_days;
    if($remainder_days > 0){
        $new_date_string = "+" . ($interval_days - $remainder_days) . " days";
    } else {
        $new_date_string = "today";
    }
    return date($output_format,strtotime($new_date_string));
}
echo nextDate('20151210',14,'Ymd') . "<br />";
echo nextDate('20150808',14,'Ymd') . "<br />";
?>

如果开始日期在遥远的位置,您也不想退回早期未来。代码更新以防止。

You also don't want to return an early date if the "start date" is in the distant future. Code updated to prevent that.