Calculation of interest and fines in PHP

Asked

Viewed 482 times

1

I’m making a code that calculates interest and fines on PHP based on the amount of days past due. However, I’m having trouble getting the date and making this calculation.

$databx = new DateTime();
$datavc = new DateTime($bxgst->getDatavencto()); //2018-10-30
$diasatraso = $databx->diff($datavc)->format('%a');

$vm = (($vlprogramado * $vlmulta) / 100);
$vj = (((($pjuros / 30) / 100) * $diasatraso) * 500) + 500;

In this case, today it shows 11 days, and shows as if it was delay, but it is not, it is 11 days in advance, not late.

What would be the best solution?

2 answers

2


Validate if the due date is less than the current date:

$databx = new DateTime();
$datavc = new DateTime($bxgst->getDatavencto()); //2018-10-30

$diasatraso = ($databx->diff($datavc)->format('%r%a') < 0) ? $databx->diff($datavc)->format('%a') : 0;

if($diasatraso > 0){
    $vm = (($vlprogramado * $vlmulta) / 100);
    $vj = (((($pjuros / 30) / 100) * $diasatraso) * 500) + 500;
}else{
    $vm = 0;
    $vj = 0;
}
  • 1

    You can replace the if for format('%r%a') that he will return the correct signal.

  • I tested it here, but it keeps showing as if it were days late. Displayed 11.

  • Thanks for the @Andersoncarloswoss :D tip

  • @JB_ if it is advanced, will the value change? If not, you need to find out if the amount received is negative, otherwise the price will be lower by paying in advance.

  • In this condition if pay in advance, the amount does not change, but if pay late there is interest and fines based on the amount of days late.

  • @JB_ I think I understand what you need, I’ll change the code

  • If the customer pays until maturity, need to be 0 the days of delay, only if there are delays that enters interest and fines based on the amount of days late.

  • Thank you for the reply

Show 3 more comments

2

If the fine will be imposed only in case of delay, I believe that validate only if the payment date is higher than that of the maturity should resolve:

$databx = new DateTime();
$datavc = new DateTime($bxgst->getDatavencto()); //2018-10-30

//caso precise das variáveis zeradas, se estiver no prazo
$diasatraso = 0;
$vm = 0;
$vj = 0;

if (date_format($databx, "Y-m-d") > date_format($datavc, "Y-m-d")) {
   $diasatraso = $databx->diff($datavc)->format('%a');

   $vm = (($vlprogramado * $vlmulta) / 100);
   $vj = (((($pjuros / 30) / 100) * $diasatraso) * 500) + 500;
}
  • 1

    Thank you for the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.