Calculate percentage between 2 numbers
I have to make a percentage system where I have 2 dates in TIMEUNIX
, being the end date ($cota->ultimo_recebimento)
and the current day that I get with the function time()
of PHP. I tried to do like this:
<?php
echo ((time() / $cota->ultimo_recebimento) * 100).' %';
?>
And so too
<?php
echo (100 - (time() / $cota->ultimo_recebimento * 100));
?>
But none gave me a certain percentage.
I just need that when passing the days he based on $cota->ultimo_recebimento
will increase the percentage. After the current date is greater than or equal to $cota->ultimo_recebimento
then it holds at 100%
I have 3 variables
$atual = time();
$primeiro = $cota->primeiro_recebimento;
$ultimo = $cota->ultimo_recebimento;
0
Author: Alisson Acioli, 2016-02-10
1 answers
Follows the calculation
// exemplo
$inicio = 1453248000; // 20 de janeiro de 2016
$fim = 1454112000; // 30 de janeiro de 2016
$atual = 1453507200; // 25 de janeiro de 2016
// no seu caso:
// $inicio = $cota->primeiro_recebimento;
// $fim = $cota->ultimo_recebimento;
// $atual = time();
$total = $fim - $inicio;
$tempoRestante = $fim - $atual;
$percentualParaTerminar = ($atual >= $fim) ? 1 : $tempoRestante / $total;
$percentualCorrido = 1 - $percentualParaTerminar;
echo $percentualParaTerminar; // saída: 0.7
echo $percentualCorrido; // saída: 0.3
echo ($percentualParaTerminar * 100) . '%'; // saída: 70%
echo ($percentualCorrido * 100) . '%'; // saída: 30%
2
Author: Berriel, 2016-02-10 16:06:16