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?

Was it helpful?

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top