2013-05-06 56 views
2

我想创建一个函数,将返回未来的付款和日期的数组。无法工作如何循环和增加我的价值

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年4月30日] => 1114.8
  • [1 - 2013年4月30日] => 1114.8
  • [2 - 2013 -04-30] => 1114.8
  • [3 - 2013年4月30日] => 1114.8
  • [4 - 2013年4月30日] => 1114.8
  • [5 - 2013年4月30日] => 1114.8
  • [6 - 2013年4月30日] => 1114.8
  • [7 - 2013年4月30日] => 1114.8
  • [8 - 2013 -04-30] => 1114.8
  • [9 - 2013年4月30日] => 1114.8
  • [10 - 2013年4月30日] => 1114.8
  • [11 - 2013年4月30日] => 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; 
} 

仍然有问题增加日期寿。
终于得到了所有的工作,谢谢你们。

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循环)。
edit3(最后工作添加工作代码)。
edit4(增加全功能功能)。

+1

for循环第一行的日期总是等于'$ startDate + 28 days' – andrewsi 2013-05-06 17:34:41

+0

您应该在循环中增加'$ i'天而不是'+ 28天'吗? – Quantastical 2013-05-06 17:34:46

+0

哪些值没有变化? – 2013-05-06 17:35:42

回答

1

你的循环中的几个值没有改变。如startdate,兴趣和日期。如果这些值未被重新分配一个新值,您将继续获得相同的输出。您可以使用var_dump($startdate, $lastpayment, $interest)来查看我在说什么。

+0

谢谢,我没有注意到 – unasAquila 2013-05-06 17:52:13

+0

我的答案投票如果是有帮助的你。很高兴我能帮忙,谢谢! :) – blackmambo 2013-05-06 17:57:17

+0

您的输入帮助我得到我的大脑顺序谢谢 – unasAquila 2013-05-06 18:20:50