Calculate percentage between 2 numbers

Asked

Viewed 1,683 times

0

I have to do a percentage system where I have 2 dates in TIMEUNIX, being the final date ($cota->ultimo_recebimento) and the current day I catch up with the function time() from PHP. I tried to do so:

<?php
echo ((time() / $cota->ultimo_recebimento) * 100).' %';
?>

and also so

<?php
echo (100 - (time() / $cota->ultimo_recebimento * 100));
?>

But none gave me a certain percentage.

I just need him to spend his days based on $cota->ultimo_recebimento increases the percentage. After the current date is greater than or equal to $cota->ultimo_recebimento so he keeps in 100%

I have 3 variables

$atual = time();
$primeiro = $cota->primeiro_recebimento;
$ultimo = $cota->ultimo_recebimento;
  • If I understand correctly, you don’t have enough data to know that percentage. To know the percentage of running time you need 3 dates: start, current and end; or 2 intervals: total time (end - start), time to end (end - current), only with these 2 values (current and end).

  • Have $cota->primeiro_recebimento yes, I forgot to put

  • @Berriel with this data, you can help ?

  • See if the answer helps you...

1 answer

2


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%
  • $current = '1455206086'; // 11 February 2016 --- $inicio = '1455188400'; // 11 February 2016 --- $end = '1456315200'; /24 February 2016 and the result is 0.98412318068868 because ?

  • Because 98.4% of the time is missing to the end... you want the % of the time to the end, or the % of the time that has already passed?

  • @Alissonacioli changed the example to be clearer

  • I left so http://pastebin.com/6y7RA8uS and it worked :) ... Thank you!

  • @Alissonacioli without problems :)

Browser other questions tagged

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