" method="post">
Mortgage Calculator
Loan amount *
Amortization length * (0-30) years
Annual interest rate * (0-30)%
Desired amortization schedule
Loan start date

* required information

30) || (intval($_POST[years]) < 0)) $error[] = "Please make sure that the Amortization length is between 0 to 30."; elseif((intval($_POST[interestRate]) > 30) || (intval($_POST[interestRate]) < 1)) $error[] = "Please make sure that the Interest rate is between 0 to 30."; if(count($error)) { echo '
'; } $error = ob_get_clean(); if(empty($error)) { ob_start(); $N = 12*$_POST[years]; $r = $_POST[interestRate]/100/12; $R = 1+$r; $monthlyPayment = $_POST[loanAmount]*$r / (1 - pow((1+$r),(-$N))); $totalInterest = ($monthlyPayment*$N)+($r*$_POST[loanAmount] - $monthlyPayment)*((pow((1+$r),$N)-1)/$r); echo "
Results"; echo ""; $y = $_POST[year]; for($t = 12; $t<=$N; $t = $t+12) { $balance = ($_POST[loanAmount]*pow($R,$t)) - $monthlyPayment*(pow($R,$t)-1)/($R-1); $balanceB = ($_POST[loanAmount]*pow($R,($t-12))) - $monthlyPayment*(pow($R,($t-12))-1)/($R-1); if($t == 12) { $principle_paid = $_POST[loanAmount] - $balance; $p = $balance; $interest_paid = ($monthlyPayment*12) - $principle_paid; } else { $principle_paid = $p - $balance; $p = $balance; $interest_paid = ($monthlyPayment*12) - $principle_paid; } $total_principle = $principle_paid + $total_principle; $t_i_p = $interest_paid + $t_i_p; $pay = $monthlyPayment*$t; $payment[$y] = round($pay, 2); $interest[$y] = round($interest_paid + $interest[$y-1], 2); $balanceR[$y] = round($balanceB, 2); $data[payment] = $payment; $data[interest] = $interest; $data[balance] = $balanceR; //print_r($principle); $y = $y + 1; } $s = str_replace('+', '_', base64_encode(gzcompress(serialize($data)))); echo ""; echo "
"; $summary = ob_get_clean(); ob_start(); if($_POST[schedule] == "Monthly") { $y = $_POST[year]; echo "

Amortization Schedule

"; echo ""; if($_POST[month]) { $key = array_search($_POST[month], $month); $m = $month[$key]; } for($t = 1; $t<=$N; $t++) { $balance = ($_POST[loanAmount]*pow($R,$t)) - $monthlyPayment*(pow($R,$t)-1)/($R-1); $balanceB = ($_POST[loanAmount]*pow($R,($t-1))) - $monthlyPayment*(pow($R,($t-1))-1)/($R-1); if($t == 1) { $principle_paid = $_POST[loanAmount] - $balance; $p = $balance; $interest_paid = $monthlyPayment - $principle_paid; } else { $principle_paid = $p - $balance; $p = $balance; $interest_paid = $monthlyPayment - $principle_paid; } $total_principle = $principle_paid + $total_principle; echo ""; $nkey = array_search($m, $month); if($nkey == 11) { $m = $month[0]; $y = $y + 1; } else $m = $month[$nkey+1]; } echo "
YearMonthBeginning BalancePrinciple PaidInterest PaidEnding Balance
$y$m" . number_format(round($balanceB, 2), 2) . "" . number_format(round($principle_paid, 2), 2) . "" . number_format(round($interest_paid, 2), 2) . "" . number_format(round($balance, 2), 2) . "
"; } if($_POST[schedule] == "Yearly") { echo "

Amortization Schedule

"; echo ""; $y = $_POST[year]; for($t = 12; $t<=$N; $t = $t+12) { $balance = ($_POST[loanAmount]*pow($R,$t)) - $monthlyPayment*(pow($R,$t)-1)/($R-1); $balanceB = ($_POST[loanAmount]*pow($R,($t-12))) - $monthlyPayment*(pow($R,($t-12))-1)/($R-1); if($t == 12) { $principle_paid = $_POST[loanAmount] - $balance; $p = $balance; $interest_paid = ($monthlyPayment*12) - $principle_paid; } else { $principle_paid = $p - $balance; $p = $balance; $interest_paid = ($monthlyPayment*12) - $principle_paid; } $total_principle = $principle_paid + $total_principle; $t_i_p = $interest_paid + $t_i_p; echo ""; $y = $y + 1; } echo "
YearBeginning BalancePrinciple PaidInterest PaidEnding Balance
$y" . number_format(round($balanceB, 2), 2) . "" . number_format(round($principle_paid, 2), 2) . "" . number_format(round($interest_paid, 2), 2) . "" . number_format(round($balance, 2), 2) . "

"; //echo ""; } $result = ob_get_clean(); } } if(empty($_POST[action])) { echo "$form"; } else { echo "$error"; echo "$summary"; echo "$form"; echo"$result"; } ?>