2013-05-06 22 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