Calculate value (R$) based on PHP time

Asked

Viewed 123 times

-2

I need to calculate the value of a service based on time. I managed to do this part, I will make the code available here. The problem is that I need to do the reverse, calculate the hours based on a value available to the user, for example:

The time of service costs: R$ 100,00. If I choose two hours of service the value will be calculated to R$ 200,00. OK, all right!

But how can I reverse this calculation? Type, the user has available as credit R $ 550,00, as I would calculate from this value the amount of X hours he has available for use?

Time-value calculation code:

<?php
$baseHora = 130.00;
$horas = "02:00:00";
$horas = explode(":", $horas);
$valor = $baseHora*$horas[0];
echo $valor;
?>

1 answer

5


I believe this is purely mathematical, merely:

Hora -----  Valor
1    -----   100
X    -----   550

If the 1 hour costs 100, then it will simply make 550/100, which will result in 5.5 hours, if you want to display in the format of 05:30:00 simply divide hours by hour, minutes and seconds.

$PrecoPorHora = 100;
$ValorDisponível = 550;

// 5.5
$HoraDisponivel = ($ValorDisponível/$PrecoPorHora);

// 05:30:00 ~ Deve ter um jeito melhor
$HoraFormatada = sprintf("%02d:%02d:%02d", floor($HoraDisponivel), (($HoraDisponivel * 3600) / 60) % 60, ($HoraDisponivel * 3600) % 60); 

Browser other questions tagged

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