-1

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?

Community
  • 1
  • 1
Rosamunda
  • 14,620
  • 10
  • 40
  • 70
  • 2
    Whether an algorithm is correct or not depends on whether it does what you intend. If you don't know the expected outputs, then it will be hard to determine whether it is right! Are you looking to calculate ordinary or compound interest? – halfer Apr 05 '14 at 21:49
  • 1
    (Downvoters: I've upvoted because this question is clear, features code and prior effort. We can't ask for expert understanding on all questions!) – halfer Apr 05 '14 at 21:54
  • Thanks for your upvote Halfer. In fact I´ve really tried to play by the rules. Even more, I thought that if it was correct could help others.. Anyway, I´ve edited the question saying what input/output is expected. Is more difficult trying to explain it in english :) – Rosamunda Apr 05 '14 at 22:04
  • Upvoted also. There's little wrong with this question. – davidf2281 Apr 05 '14 at 22:41

1 Answers1

1

No, it is wrong.

$mesesEnAnios = (($tYear - $fYear) -1) * 12;

That is for example: (2013 - 2012 - 1) = 0

0 * 12 = 0, while its obvious 12.

Then you do

$mesesScattered = (12 - $fMonth) + $tMonth;

So, lets say we have 05-2013 (month - year) and 05-2012. The result should be: 12 months. Lets assume you fixed the first line I pointed out, we get 12 months from that. On your second line, you get 12 - 5, which is 7. Result 7+12 = 19.. which is again obvious wrong.

To make it easy, ill just give you the code..

$months = (($tYear - $fYear ) * 12) + ($tMonth- $fMonth);

You first check if there are any extra's years and do this amount * 12 for the 12 months we have. 0 years give 0 months, 1 year gives 12 months etc. After that we check the months. If the first value is 8 months and the second value is 6 months, we have 2 months in total and add this value on top of the value we got from the years calculation.

That is the month part, for the other part I cannot help you since I do not know the calculation. Yet assuming you have 0 experience in math and PHP (like you said), I would recommend to manually calculate it, and compare it with the result from the script.

W van Rij
  • 536
  • 2
  • 16