Php 如何使用strotime动态增加日期?

Php 如何使用strotime动态增加日期?,php,for-loop,increment,strtotime,Php,For Loop,Increment,Strtotime,我需要从一个起点开始26次约会。从上一个日期开始的下一个日期。硬编码一切都是疯狂的。。。所以我想知道我怎样才能动态地做到这一点?有更聪明的方法吗?我希望在第二次约会后增加工资。也许有一个for循环 <?php //incrementing dates for bi-weekly (26 periods// 26 dates) $firstdate = strtotime("+17 days", strtotime("2017-04-03"));//1 $i

我需要从一个起点开始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;
}
//使用第一个日期初始化数组
$dates=数组(strotime(“+17天”),strotime(“2017-04-03”);
//现在循环26次以获得接下来的26个日期
对于($i=1;$i这个怎么样:

// 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;
}
//使用第一个日期初始化数组
$dates=数组(strotime(“+17天”),strotime(“2017-04-03”);
//现在循环26次以获得接下来的26个日期

对于($i=1;$i来说,最简单的方法可能是使用数组

$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>";
$myDates=[];
$firstdate=STROTIME(“17天”),STROTIME(“2017-04-03”);
数组推送($myDates,date(“Y-m-d,$firstdate));

对于($i=0;$i来说,最简单的方法可能是使用数组

$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>";
$myDates=[];
$firstdate=STROTIME(“17天”),STROTIME(“2017-04-03”);
数组推送($myDates,date(“Y-m-d,$firstdate));

对于($i=0;$iTry并首先用语言标记它,然后用更深奥的语言标记它。您希望增加日期的数量是多少?是固定数量还是变化数量?@user3299379:第二个日期之后总是+14天,因为这与数据库相关;为什么不在插入/更新期间直接在查询中这样做,然后在mysql中也添加/减法,如果需要,使用
WHERE
子句?这就是我从中得到的。如果我们知道这到底是为了什么,也许mysql解决方案会比php更好。@SebastianFarham这篇关于堆栈的问答会更好地解释/概述我的意思和手动参考,请尝试先用语言标记它,然后再添加更多注释oteric things after.what’s amount you want increment the dates by?它是一个常量还是一个变化量?@user3299379:第二个日期之后,它将始终是+14天,因为这与数据库相关;为什么不在insert/update期间直接在查询中执行此操作,然后在mysql中使用
WHERE
cl添加/减法如果需要的话可以吗?这就是我从中得到的。如果我们知道这到底是为了什么,也许mysql解决方案会比php更好。@SebastianFarham这篇关于堆栈的问答会更好地解释/概述我的意思和手动参考