2017-04-02 79 views
1

我需要从一个起点获得26个日期。下一个日期从前一个日期开始。这将是疯狂的硬编码的一切...所以我想知道如何动态地做到这一点?有更聪明的方法吗?我期待在第二次约会后增加。也许有一个for循环?如何使用strtotime动态增加日期?

<?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>'; 
    ?> 
+1

尝试和标签这与语言首先最简单的方法,经过那么多深奥的东西。 – tadman

+0

你想通过增加日期的数量是多少?这是一个恒定的金额还是一个变化的金额? – Jpsh

+0

@ user3299379:在第二次约会之后,它总是会是+14天 –

回答

3

如何:

// 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; 
} 
1

可能做到这将是一个数组

$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>";