PHP: Calculating months and interest rate. Is this solution correct? It does work -
i want calculate how many months have passed since x1 date x2 date, , calculate how have pay given monthly interest rate.
the script should: amount of debt (capital), month , year when debt originated , until should calculated. not day, month , year. , output should total generated in interests, , total months between 2 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 i've done:
if($_server['request_method']=='post') { //i try , obtain how many months have between 2 months $mesesenanios = (($tyear - $fyear) -1) * 12; $mesesscattered = (12 - $fmonth) + $tmonth; $mesestotales = $mesesenanios + $mesesscattered; //then calculate interest i'll have pay $totalcapital = $capital * ($interes * $mesestotales) / 100; echo 'son $'.$totalcapital.' en '.$mesestotales.' meses.'; ...
i've tried script , works. don't know php (nor math) , don´t know if work.
i've researched other solutions here @ so, think mine little easier - @ least understand :) - maybe it´s because it's not correct?
no, wrong.
$mesesenanios = (($tyear - $fyear) -1) * 12;
that example: (2013 - 2012 - 1) = 0
0 * 12 = 0, while obvious 12.
then do
$mesesscattered = (12 - $fmonth) + $tmonth;
so, lets have 05-2013 (month - year) , 05-2012. result should be: 12 months. lets assume fixed first line pointed out, 12 months that. on second line, 12 - 5, 7. result 7+12 = 19.. again obvious wrong.
to make easy, ill give code..
$months = (($tyear - $fyear ) * 12) + ($tmonth- $fmonth);
you first check if there extra's years , amount * 12 12 months have. 0 years give 0 months, 1 year gives 12 months etc. after check months. if first value 8 months , second value 6 months, have 2 months in total , add value on top of value got years calculation.
that month part, other part cannot since not know calculation. yet assuming have 0 experience in math , php (like said), recommend manually calculate it, , compare result script.
Comments
Post a Comment