dtzjvj3915 2018-07-17 09:39
浏览 81
已采纳

借助for循环在php中的贷款分期付款

I'm working on some loan project with php which installments are created by the code below. Note:the first installment should be dated to the next month from $loanyearacc and $loanmonthacc. for example if the loan accepted in 12/01/2018,the first installment is 01/01/2019 the code is:

$loanmonthacc=12;
$loanyearacc=2018;
$loanreqmonth=18;

for($i=$loanmonthacc+1;$i<=$loanmonthacc+$loanreqmonth+1;$i++){
    if($i%12==0){
        $m=12;
        }
    else{
        $m=$i%12;
        }   
    echo 'i: '.$i.'year: '.$loanyearacc.'month: '.$m.'<br>';
    if($i%12==0){
        $loanyearacc++;
        }
    if($i%13==0){
        $loanyearacc++;
        }
}

Code above works fine just in case that $loanmonthacc is not equal to 12. but if $loanmonthacc equals to 12, JUST the first installment would be next month BUT the year wont increase. can anyone please help me? Sorry for my weak english guys :)

  • 写回答

1条回答 默认 最新

  • doumeng3080 2018-07-17 11:24
    关注

    You simply run the counter YEAR | MONTH for the number of required months - exactly as the mileage in your car, i.e. the next month after 12 is again 1 but then you also increase the year:

    $loanmonthacc=12;
    $loanyearacc=2018;
    $loanreqmonth=18;
    
    $installment = 1;
    while($loanreqmonth)
    {
      $loanmonthacc++;
      if($loanmonthacc > 12)
      {
        $loanmonthacc = 1;
        $loanyearacc++;
      }
      echo "Installment $installment = $loanmonthacc / $loanyearacc <br>";
      $loanreqmonth--;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播