문제

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