How can I calculate percentage between two time values in php: Completed time 4min. and 35sec. Maximum time 11min. What would the remaining 6min and 25sec be in percentage?
Asked
Active
Viewed 2,335 times
-4
-
2`$time / 100` it's basic math or `$time * 100`. The / is the "divide" arithmetic operator and `*` multiply. Percent is per 100's. Have you attempted anything, or code to show us? – Funk Forty Niner Mar 10 '15 at 14:42
-
convert maximum time to seconds, convert actual time to seconds, and divide the numbers. It isn't too hard. – Davis Broda Mar 10 '15 at 14:44
-
Downloading is almost 43% complete – Amit Verma Mar 10 '15 at 14:45
-
http://stackoverflow.com/q/5910755/ --- http://stackoverflow.com/q/365191/ --- http://php.net/manual/en/function.date-diff.php – Funk Forty Niner Mar 10 '15 at 14:46
2 Answers
3
First, make those minutes into seconds:
4 min 35 sec = 275 seconds
11 min = 660 seconds
Your percentage of remaining time will be (275 / 660) * 100. The percentage of time left would be ((660 - 275) / 660) * 100. Of course, that's all in seconds. Don't know how you are receiving that time in php, but it might look like:
$maxTime = 660;
$timeTaken = 275;
$percentage = ($timeTaken / $maxTime) * 100;
// To get percentage of time left
$percentLeft = (($maxTime - $timeTaken) / $maxTime) * 100;

Captain Hypertext
- 2,446
- 4
- 27
- 36
2
If you have minutes and second separately, it will be:
$min = 4;
$sek = 35;
$maxmin = 11;
$percentage = round((60*$maxmin - (60*$min + $sek))/($maxmin*60)*100,2);
result is 58,33

n-dru
- 9,285
- 2
- 29
- 42
-
158,33 is completely wrong. Not even 5 minutes of 11 minutes is over 50%. – Christian Lundahl Mar 10 '15 at 14:46
-
1