我正在尝试创建一个函数,该函数将返回一组未来付款和日期。
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] => 1114.8
- [1 - 2013-04-30] => 1114.8
- [2 - 2013-04-30] => 1114.8
- [3 - 2013-04-30] => 1114.8
- [4 - 2013-04-30] => 1114.8
- [5 - 2013-04-30] => 1114.8
- [6 - 2013-04-30] => 1114.8
- [7 - 2013-04-30] => 1114.8
- [8 - 2013-04-30] => 1114.8
- [9 - 2013-04-30] => 1114.8
- [10 - 2013-04-30] => 1114.8
- [11 - 2013-04-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(添加了全功能功能)。