Question

I'm trying to work out a simple algorithm to get a percentage of how far the current time is, in-between two timestamps. (this is confusing to write so probably confusing to read, sorry)

I'm using PHP and Unix timestamps "time()"

So far I've got,

$percentCompleted = ((time() - $startTimestamp) / $endTimestamp) * 100;

Any ideas?

Était-ce utile?

La solution

You need normalize result by difference from start timestamp to end timestamp.

$percentCompleted = ((time() - $startTimestamp) / ($endTimestamp - $startTimestamp)) * 100;

Of course you can add some protecting against invalid input like time() > $endTimestamp

$percentCompleted = ((min(time(),$endTimestamp)  - $startTimestamp) / ($endTimestamp - $startTimestamp)) * 100;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top