I am trying to create a function that will return an array of future payment and dates.
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;
}
This all works as expected and intended but when I add a for loop to increase the date by 28 days, and do the math for the amount remaining. I am lost as to what I am doing wrong.
This is what I am hoping to happen.
//----------------------------------------------
$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;
//----------------------------------------------
But cant work out how to do it in a for loop.
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;
}
Outputs
- [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
- ...
But this just adds the same value over and over.
Thanks to BlackMambo i have got to this -
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;
}
Still having trouble increment the date tho.
Finally got it all working thanks 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;
}
The Final working function
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;
}
edit(added syntax correction + output).
edit2(Updated with new for loop).
edit3(Finally working added working code).
edit4(Added fully functional function).