Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Created June 22, 2013 04:12
Show Gist options
  • Save andrewrcollins/5835850 to your computer and use it in GitHub Desktop.
Save andrewrcollins/5835850 to your computer and use it in GitHub Desktop.
Regular Payment at Regular Interval (PMT)
<?php
/**
* Future Value Interest Factor (FVIF)
*
* @param float $interest annual interest
* @param int $periods periods
* @throws InvalidArgumentException if $interest is not float
* @throws InvalidArgumentException if $periods is not int
* @return float future value interest factor
*/
function fvif($interest, $periods)
{
if(!is_float($interest))
{
throw new InvalidArgumentException('$interest is not float');
}
if(!is_int($periods))
{
throw new InvalidArgumentException('$periods is not int');
}
if($periods == 0)
{
return 1;
}
$interest_factor = $interest + 1;
if($interest_factor == 0)
{
return 0;
}
if($periods == 1)
{
return $interest_factor;
}
if($periods < 0)
{
$periods = -$periods;
$interest_factor = 1 / $interest_factor;
}
return pow($interest_factor, $periods);
}
/**
* Regular Payment at Regular Interval (PMT)
*
* @param float $interest annual interest
* @param int $periods periods
* @param float $present_value present value
* @param float $future_value future value
* @throws InvalidArgumentException if $interest is not float
* @throws InvalidArgumentException if $periods is not int
* @return float regular payment at regular interval
*/
function pmt($interest, $periods, $present_value, $future_value)
{
if(!is_float($interest))
{
throw new InvalidArgumentException('$interest is not float');
}
if(!is_int($periods))
{
throw new InvalidArgumentException('$periods is not int');
}
if($interest == 0)
{
$pmt = -1 * ($future_value + $present_value) / $periods;
}
else
{
$n = ($present_value + $future_value);
$d = fvif($interest, $periods) - 1;
$pmt = -1.0 * (($n / $d) + $present_value) * $interest;
}
return $pmt;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment