如何使用 strtotime 动态增加日期?

How to dynamically increment dates using strtotime?

我需要从起点获取 26 个日期。下一个日期从上一个日期开始。硬编码一切都是疯狂的......所以我想知道我怎样才能动态地做到这一点?有更聪明的方法吗?我希望在第二次约会后增加。也许用 for 循环?

    <?php
    //incrementing dates for bi-weekly (26 periods// 26 dates)
    $firstdate = strtotime("+17 days", strtotime("2017-04-03"));//1
    $i = date("Y-m-d", $firstdate); echo date("Y-m-d", $firstdate);//echo for testing
    echo'<br>';
    $seconddate =strtotime("+14 days", strtotime($i));//2
    $ii = date("Y-m-d", $seconddate); echo date("Y-m-d", $seconddate);//echo for testing
    echo'<br>';
    ?>

这个怎么样:

// initialize an array with your first date
$dates = array(strtotime("+17 days", strtotime("2017-04-03")));

// now loop 26 times to get the next 26 dates
for ($i = 1; $i <= 26; $i++) {
    // add 14 days to previous date in the array
    $dates[] = strtotime("+14 days", $dates[$i-1]);
}

// echo the results
foreach ($dates as $date) {
    echo date("Y-m-d", $date) . PHP_EOL;
}

可能最简单的方法是使用数组

$myDates = [];
$firstdate = strtotime("+17 days", strtotime("2017-04-03"));
array_push($myDates, date("Y-m-d",$firstdate));
for($i=0;$i<25;$i++){
    $lastdate = $myDates[$i];
    $nextdate = strtotime("+14 days", strtotime($lastdate));
    array_push($myDates,date("Y-m-d",$nextdate));
}    

echo "<pre>".var_dump($myDates)."</pre>";