无法弄清楚如何循环和增加我的价值观

时间:2013-05-06 17:30:14

标签: php date for-loop

我正在尝试创建一个将返回未来付款和日期数组的函数。

function getFuturePayments(){
    $intMap = array();

    $startVal     = 1000.00;
    $startDate    = '2013-04-02';
    $interest     = 2.843; //(39.9% apr)
    $minPayment   = 62.92;
    $intMap['00 - ' . date('o-m-d', strtotime($startDate))] = $startVal;

    //----------------------------------------------
    $int = $startVal * ($interest / 100);
    $lastPayemt  = $startVal + $interestAmount - $minPayment;

    $intMap[sprintf('%02s', 1) .' - 2013-04-30'] = sprintf('%0.2f', $lastPayemt);

 return $intMap;

}

这一切都按预期和预期工作,但是当我添加for循环以将日期增加28天时,并对剩余的数量进行数学计算。我迷失了我的错误。

这是我希望发生的事情。

//----------------------------------------------
$int2 = $lastPayemt * ($interest / 100);
$interestAmount2 = $this->roundUp($int2, 2);
$nextPayment = $lastPayemt + $interestAmount2 - $minPayment;

$intMap['02 - 2013-05-28'] = $nextPayment;
//----------------------------------------------
$int2 = $nextPayment * ($interest / 100);
$interestAmount2 = $this->roundUp($int2, 2);
$nextPayment = $nextPayment + $interestAmount2 - $minPayment;

$intMap['03 - 2013-06-25'] = $nextPayment;
//----------------------------------------------
$int2 = $nextPayment * ($interest / 100);
$interestAmount2 = $this->roundUp($int2, 2);
$nextPayment = $nextPayment + $interestAmount2 - $minPayment;

$intMap['04 - 2013-07-23'] = $nextPayment;
//----------------------------------------------

但是无法解决如何在for循环中完成它。

for($i=0; $i < 27; $i++){ //hard coded for now      
    $date = date('o-m-d', strtotime($startDate .' + 28 days'));
    $int2 = $lastPayemt * ($interest / 100);
    $interestAmount2 = $this->roundUp($int2, 2);
    $nextPayment = $lastPayemt + $interestAmount2 - $minPayment;

   $intMap[ $i .' - ' . date('o-m-d', strtotime($date)) ] = $nextPayment;
}

输出

  
      
  • [0 - 2013-04-30] =&gt; 1114.8
  •   
  • [1 - 2013-04-30] =&gt; 1114.8
  •   
  • [2 - 2013-04-30] =&gt; 1114.8
  •   
  • [3 - 2013-04-30] =&gt; 1114.8
  •   
  • [4 - 2013-04-30] =&gt; 1114.8
  •   
  • [5 - 2013-04-30] =&gt; 1114.8
  •   
  • [6 - 2013-04-30] =&gt; 1114.8
  •   
  • [7 - 2013-04-30] =&gt; 1114.8
  •   
  • [8 - 2013-04-30] =&gt; 1114.8
  •   
  • [9 - 2013-04-30] =&gt; 1114.8
  •   
  • [10 - 2013-04-30] =&gt; 1114.8
  •   
  • [11 - 2013-04-30] =&gt; 1114.8
  •   
  • ...
  •   

但这只是反复增加相同的值。

感谢BlackMambo我必须这样做 -

for($i=0; $i < 27; $i++){ //hard coded for now    
    $date = date('o-m-d', strtotime($startDate . ' + 28 days'));
    $int = $lastPayemt * ($interest / 100);
    $interestAmount = $this->roundUp($int, 2);  

    $nextPayment = $lastPayemt + $interestAmount - $minPayment;
    $lastPayemt = $nextPayment + $interestAmount - $minPayment;         

    $intMap[ $i .' - ' . date('o-m-d', strtotime($date)) ] = $nextPayment;
}

仍然无法增加日期。
最后得到了所有工作,感谢Guys。

for($i=1; $i < 27; $i++){ //hard coded for now 

    $lastDate = date('o-m-d', strtotime($startDate));
    $lastDate = date('o-m-d', strtotime($lastDate . ' + 28 days'));
    $startDate = date('o-m-d', strtotime($lastDate));

    $int = $lastPayemt * ($interest / 100);
    $interestAmount = $this->roundUp($int, 2);  

    $nextPayment = $lastPayemt + $interestAmount - $minPayment;
    $nextPayment = $nextPayment + $interestAmount - $minPayment;
    $lastPayemt = $nextPayment;                 


    $intMap[sprintf('%02s', $i).' - '.date('o-m-d',strtotime($lastDate))]=$nextPayment;


}

最终工作职能

function getFuturePayments($startAmount, $startFromDate, $baseInterest, $minPayment){
    $intMap = array();

    $startVal     = $startAmount;
    $startDate    = $startFromDate;
    $interest     = $baseInterest; //(39.9% apr / 34.1% compouned)
    $minPayment   = $minPayment;

    $intMap['00 - ' . date('o-m-d', strtotime($startDate))] = $startVal;
    //-----------------------------------------------------------------
    $int = $startVal * ($interest / 100);
    $interestAmount = roundUp($int, 2);
    $lastPayemt = $startVal + $interestAmount - $minPayment;

    $startDate = date('o-m-d', strtotime($startDate . ' + 28 days'));

    for($i=1; $lastPayemt > 0; $i++){       
        $int = $lastPayemt * ($interest / 100);
        $interestAmount = roundUp($int, 2);
        $nextPayment = $lastPayemt;

        $lastDate = date('o-m-d', strtotime($startDate));
        $lastDate = date('o-m-d', strtotime($lastDate));
        $startDate = date('o-m-d', strtotime($lastDate . ' + 28 days'));

        $intMap[ sprintf('%02s', $i) . ' - ' . date('o-m-d', strtotime($lastDate)) ] =  sprintf('%0.2f', $nextPayment);

        $lastPayemt = $nextPayment + $interestAmount - $minPayment;         
        }
    return $intMap;
    }
//roundUp function from an answer here http://stackoverflow.com/questions/8239600/rounding-up-to-the-second-decimal-place
function roundUp ($value, $precision){ 
    $pow = pow(10, $precision); 
    return (ceil($pow * $value) + ceil($pow * $value - ceil($pow * $value))) / $pow; 
}

编辑(添加语法修正+输出)。
edit2(更新为for for循环)。
edit3(最后工作添加了工作代码) edit4(增加了全功能功能)。

1 个答案:

答案 0 :(得分:1)

循环中的几个值没有改变。如startdate,兴趣和日期。如果这些值未重新分配新值,您将继续获得相同的输出。

//example ONLY,
$startDate = $lastDate;
$lastpayment = xyz;

您甚至可以使用var_dump($startdate, $lastpayment, $interest)查看我在说什么。