I need help converting the following excel formula to PHP
PV(.0588/12,300,868.0583333)
The output I am expecting is 136,275.88
, but the output that I am getting is 590573.166
. I have spent hours on it but I can't seem to find the solution.
This is my code
function FV($rate = 0, $nper = 0, $pmt = 0, $pv =0, $type = 0)
{
// Validate parameters
if ($type != 0 && $type != 1) {
return False;
}
// Calculate
if ($rate != 0.0) {
return -$pv * pow(1 + $rate, $nper) - $pmt * (1 + $rate * $type) * (pow(1 + $rate, $nper) - 1) / $rate;
} else {
return -$pv - $pmt * $nper;
}
} // function FV()
echo FV(.0588/12, 300, -868.06);
I have gone through the similar post before but that does not solve.
I have also gone through the PHP site but that does not help either.
Your algorithm is for
FV
, but apparently you want it forPV
. You're in luck as Alejandro Pedraza already made a PEAR packageMath_Finance
for this. Here's the function extracted to prove that it works (you should include the fullFinance.php
with its copyright info in your project!).Usage:
DEMO
Note:
Like previously mentioned, you should use the full
Finance.php
file from the PEAR page in order for this to work perfectly (as you might get notices of undefined constants, and it can't raise errors). You can find the package HERE.