Reputation: 1411
Hello people :) How can i get the percent between to unix timestamps (I need to use it to my progress bar)
If i have for example 1303248948 and 1303252200. How can i the found out what the percent is out of 100% percent.
So my progress bar constantly can change the width when the first timestamp comes closer to the second timestamp.
So it will be: width="1%" and width="2%" and so on, until it reach 100% in width.
Hope someone can help me! ;-)
Upvotes: 2
Views: 3537
Reputation: 28125
Actually, you need 3 time stamps:
starting stamp, current stamp (progress), finish stamp
I'm assuming the current stamp is the current time()
. In which case;
$begin= strtotime('-2 weeks');
$now = time();
$end = strtotime('+2 weeks');
$percent = ($now-$begin) / ($end-$begin) * 100;
Note: In the above example, $percent
should be 50%.
Upvotes: 17
Reputation: 7553
This is actually a pretty simple range mapping problem. Given the value t in the range [a, b] you need to map it to an equivalent value in the range [c, d].
First you'll want to map t into the range [0, 1]
x = (t - a) / (b - a)
Second you'll want to map that value x to the range [c, d]
t' = x * (d - c) + c
This is actually a pretty handy general equation that I use frequently for all kinds of problems and remembering how to construct it is really handy.
Upvotes: 1