This supposed to be easier to solve or google the answer, but I just can't get it well done. May be I'm just stuck:
This is what I tried:
$now = time();
// i.e Improve police arriving time from 15 mins to 10 mins
$array_ini = explode(':',$ini_value); // "00:15:00" in my example (15 mins)
$array_desired = explode(':',$desired_value); // "00:10:00" in my example
$ini = $now-mktime($array_ini[0],$array_ini[1],$array_ini[2]);
$des = $now-mktime($array_desired[0],$array_desired[1],$array_desired[2]);
$percent = (1-$ini/$des)*100;
But all I get is .47% as improvement and my logic says that it really is a 33% improvement. What am I doing wrong?
It's much easier to just deal with minutes:
$ini_mins = 15;
$desired_mins = 10;
$improvement_mins = $ini_mins - $desired_mins;
$percent = ($improvement_mins / $ini_mins) * 100;
print_r($percent);
It is indeed straight forward and easier to just deal with minutes, as Ryan said in his answer.
But to add to it, what you are doing wrong - you are deducting unix time of 01 Jan 1970 00:10:00 and 01 Jan 1970 00:15:00 from say unix time of 15 Sep 2015 19:00:00. Of course the percentage difference between these two numbers would be small. You are doing something like this
num1 = 100000 - 10
num2 = 100000 - 15
percentage num1/num2 is wrong way to find percentage diff between 10 and 15; and also it is going to be much smaller than 33%.
plus you have a code bug. The array is called $array_desired but you reference $array_des in mktime.
Ok. I guess that Amit opened my mind. Based on his comment, I post the right answer:
// This line is not needed any more
//$now = time();
// i.e Improve police arriving time from 15 mins to 10 mins
$array_ini = explode(':',$ini_value); // "00:15:00" in my example (15 mins)
$array_desired = explode(':',$desired_value); // "00:10:00" in my example
// Time must to be based on Jan, 1 1970
// Hours are from 1 to 23, so must be increased by 1
$ini = mktime($array_ini[0]+1,$array_ini[1],$array_ini[2],1,1,1970);
$des = mktime($array_desired[0]+1,$array_desired[1],$array_desired[2],1,1,1970);
$percent = (1-$des/$ini)*100;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.