Skip to content

Instantly share code, notes, and snippets.

@otar
Created December 8, 2010 10:00
Show Gist options
  • Save otar/733103 to your computer and use it in GitHub Desktop.
Save otar/733103 to your computer and use it in GitHub Desktop.
Calculates monthly or total payable amount of the loan
<?php
/**
* Calculates monthly or total payable amount of the loan.
*
* @param integer $amount Loaned amount, default is 1000
* @param integer $percent Percent in which amount should be calculated, default is 15
* @param integer $months Loan duration in months, default is 12 months (1 year)
* @param boolean $total If positive value is passed function returns total payable amount instead of monthly
* @return float Returns 2 decimal float number
*/
function calculate_loan($amount = 1000, $percent = 15, $months = 12, $total = FALSE)
{
$rate = 1 - (1 / pow((1 + $percent / 1200), $months));
$monthly = $amount * (($percent / 12) / 100) / $rate;
$total AND $monthly *= $months;
return round($monthly, 2);
}
echo calculate_loan(1000, 15, 12);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment