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?

有帮助吗?

解决方案

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top