T
T
Timur Kalimullin2014-11-12 23:32:38
PHP
Timur Kalimullin, 2014-11-12 23:32:38

How to find out the percentage of elapsed time?

I display to the visitor the amount of % elapsed time as a loading bar.
There is:

  • $NOW = unixtime, current time
  • $START = unixtime, the moment the action started
  • $INTERVAL = interval, 600 (10 min)
  • $TOTAL = total percent should be on the scale
To calculate progress I write:
$progress  = $TOTAL - ($TOTAL- ($NOW - $START) / $INTERVAL * $TOTAL);
/*echo 'Осталось: ' . date('i мин. s сек.', $START + $INTERVAL  - $NOW);*/

Did I make a mistake when calculating % progress?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladlen Grachev, 2014-11-13
@gwer

Well, if you open the brackets, then the presented formula is equivalent to the following:
Everything else is correct.

K
Koss1024, 2014-11-13
@Koss1024

As far as I understand, you display the percentage of the past 10 minutes from the time START to NOW
. I just don’t understand what TOTAL is? Is it 100% or what?
that is, time elapsed:
passed = now - start
This is:
percentage = 100% * passed/interval = 100 * (now-start)/interval // final formula
of the percentage of your INTERVAL variable.
As far as I understand this is progress.
To check the correctness write tests:
Check the boundary conditions:
at 0 elapsed time:
passed = now - start = 0
percentage = 100% * 0/interval = 0%
at time equal to now = interval+start
passed = now - start = interval+start -start=interval
percentage = 100% * passed/interval = 100% * interval/interval = 100%
in the middle will be 50% respectively.
Did you want to get it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question