I want to calculate how many months have passed since an X1 date to an X2 date, and to calculate how much will I have to pay with a given monthly interest rate.
The script should: Get the amount of a debt (capital), the month and year when the debt was originated and up until it should get calculated. Not the day, just the month and the year. And the output should be the total generated in interests, and the total months between the two dates.
I have these initial variables:
$tMonth = $_POST['tmes'];
$tYear = $_POST['tanio'];
$interes = $_POST['interes'];
$fMonth = $_POST['fmes'];
$fYear = $_POST['fanio'];
$capital = $_POST['capital'];
And this is what I've done:
if($_SERVER['REQUEST_METHOD']=='POST') {
//I try and obtain how many months do I have between the two months
$mesesEnAnios = (($tYear - $fYear) -1) * 12;
$mesesScattered = (12 - $fMonth) + $tMonth;
$mesesTotales = $mesesEnAnios + $mesesScattered;
//Then I do calculate the interest I'll have to pay
$totalCapital = $capital * ($interes * $mesesTotales) / 100;
echo 'Son $'.$totalCapital.' en '.$mesesTotales.' meses.';
...
I've tried this script and it works. But I don't know much about PHP (nor math) and I don´t know if this will always work.
I've researched other solutions here at SO, but I think mine is a little easier - at least I do understand it :) - maybe it´s because it's not correct?