Version: 1.0
Type: Full Script
Category: Money
License: GNU General Public License
Description: Small payment calculator for loans of 40 years or less
<html>
<body>
<?
/*
by Gary Taylor (garytayl@wwclass.com) of World Wide Classified, marketing
specialist, sales and hosting. Soon offering low cost hosting providing
UNIX accounts supporting PHP3, MySQL, SSL, Ecommerce (no telnet access).
Inspired by William Lucking (will@ahnet.net), where I found the
financial function. Thanks to Richard D Foerster for eliminating those
pesky error messages upon first loading.
Working page at http://www.wwclass.com/Loans/loancalc.wwc
VARIABLES:
$per = number of periods per year. For instance, if you are
making payments monthly then this number is 12.
$amount = total number of periods. For instance, if you are compounding
monthly for 5 years, this number would be 60 (12 months x 5 years).
$R = annual interest rate
$principal = current principal of a loan
*/
error_reporting(0);
function PaymentCalc ($per,$amount,$R,$principal) {
$Z = 1 / (1 + ($R/$per));
return ((1 - $Z) * $principal) / ($Z * (1 - pow($Z,$amount)));
}
// this echoes the input form to the browser
echo '<CENTER><H2>Courtesy of <A HREF="http://www.wwclass.com">World Wide
Classified</A></H2></CENTER>';
echo "<FORM METHOD=POST ACTION=\"$PHP_SELF\">\n";
echo '<TABLE><TR><TD>';
echo '<CENTER>Number of Payments<BR> (3 years = 36 payments)<BR>';
echo '<INPUT TYPE="text" NAME="amount" VALUE="'.$amount. '"></TD>';
echo '<TD VALIGN=BOTTOM><CENTER>Annual Interest Rate<BR>';
echo '<INPUT TYPE="text" NAME="R" VALUE="'.$R. '"></TD>';
echo '<TD VALIGN=BOTTOM><CENTER>Amount Financed<BR>';
echo '<INPUT TYPE="text" NAME="principal" VALUE="'.$principal. '"></TD></TR>
echo '<TR><TD COLSPAN=2><CENTER><INPUT TYPE="submit"
VALUE = "Press for Monthly Payment"><INPUT TYPE="reset"></FORM>';
$pmt2= PaymentCalc(12,$amount,$R,$principal);
echo '<TD VALIGN=TOP><FORM METHOD=POST ACTION="http://wwclass.com/Loans/amor
echo '<INPUT TYPE = HIDDEN NAME=amount VALUE="'.$amount. '">';
echo '<INPUT TYPE = HIDDEN NAME=R VALUE="'.$R. '">';
echo '<INPUT TYPE = HIDDEN NAME=principal VALUE="'.$principal. '">';
echo '<INPUT TYPE = HIDDEN NAME="Pmt2" VALUE="'.$pmt2. '">';
echo '<INPUT TYPE = submit VALUE = "Amortization Table">';
echo '</TD></TR>';
if($R>1){ // To accept interest rate as whole number or /100
$R=$R/100;
}
// To strip out commas and dollar signs.
$pattern= '/,/';
$pattern2= '/\$/';
$replace= '';
$principal = preg_replace ($pattern, $replace, $principal);
$principal = preg_replace ($pattern2, $replace, $principal);
if($R == ''){
$R=0;
}
if($amount == ''){
$amount=0;
}
if($principal == ''){
$principal=0;
}
$pmt= PaymentCalc(12,$amount,$R,$principal);
$Pmt = number_format($pmt, 2); // To format into dollars and cents.
echo '<TR><TD COLSPAN=2>';
echo 'Your Monthly payment will be: ';
print "<FONT SIZE=+2 COLOR=RED>\$$Pmt</FONT>";
echo '</TD></TR></TABLE></FORM>';
if($R == '0'){ // If they leave the interest rate blank
echo "<H2><FONT COLOR=RED>Please enter interest rate</FONT></H2>\n";
}
if($amount == '0'){ // If they leave number of payments blank
echo "<H2><FONT COLOR=RED>Please enter how many payments</FONT></H2>\n";
}
if($principal == '0'){ // If they leave amount financed blank
echo "<H2><FONT COLOR=RED>Please enter amount financed</FONT></H2>\n";
}
?>
</body>
</html>