Calculation of interest and penalties in PHP

1

I am making a code that calculates interest and penalties on PHP based on the amount of days late. But I'm having trouble getting the date and doing 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?

    
asked by anonymous 19.10.2018 / 15:46

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;
}
    
19.10.2018 / 15:52
2

If the fine will be applied only in case of delay, I believe that only validate if the payment date is greater than the due date 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;
}
    
19.10.2018 / 16:11