php - 为什么在执行此 ff 循环后我总是得到当前月份及其日期?

php - why I'm always getting the present month with its date after executing this ff loop?

我有以下循环,它会获取具有给定持续时间和开始日期的日期。

$durationMonth =$_POST['pay_months']; //how many months to pay
$count = ($durationMonth * 30);
$day = 1;
for ($i=1; $i<=$count; $i++)
{ 
    $day+=1; 
    $start_date=date("y-m-d");
    $start_date=strtotime(date("y-m-d",strtotime($start_date))." + $day day");
    $start_date=date("y-m-d",$rel_date);

  echo "<tr>
 <td id ='row'> ".$i."</td> 
 <td id ='row'> ".$start_date." </td>
 </tr> ";
}

这个循环不起作用。如果我设置 ff:

 $start_date =  '2015-01-01'
 $duration = 1

即使我尝试了多少次,这总是给出从 15-03-0415-04-02 的日期。我应该如何修改我的代码?感谢帮助

我标记的是 duplicate .

所以你可以试试:

$durationMonth =$_POST['pay_months']; //how many months to pay
$start_date=strtotime (date("y-m-d"));
for ($i=1; $i<=$durationMonth; $i++)
{ 
  $start_date = date("Y-m-d", strtotime("+1 month", $start_date));
  echo "<tr>
 <td id ='row'> ".$i."</td> 
 <td id ='row'> ".$start_date." </td>
 </tr> ";
}

如果日期超过 28,您唯一应该担心的事情。

如果您可以接受此解决方案以避免将 29、30、31 用作约会日期:

$durationMonth =$_POST['pay_months']; //how many months to pay
$start_date=strtotime (date("y-m-d"));
if (date('j',$start_date)>28)
   $start_date=strtotime (date("y-m-").'28');
for ($i=1; $i<=$durationMonth; $i++)
{ 
  $start_date = strtotime("+1 month", $start_date);
  echo "<tr>
 <td id ='row'> ".$i."</td> 
 <td id ='row'> ". date("Y-m-d", $start_date)." </td>
 </tr> ";
}