我需要从起点获得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>';
?>
答案 0 :(得分: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 :(得分: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>";