Jump to content

time math - calculate percentage of time


gscharlemann

Recommended Posts

I am having trouble computing the percentage of time spent doing an activity compared to the overall time.  I do:

$percent_time = ($time_swimming / $overall_time);
echo "$time_swimming / $overall_time<br>";
echo "percent swim = ".number_format($percent_time,4)."<br>";

I get the following:

 

00:17:22 / 01:33:45

percent swim = 0.0000

 

I'm stuck.

 

Link to comment
https://forums.phpfreaks.com/topic/179570-time-math-calculate-percentage-of-time/
Share on other sites

A computer has no concept of time.

 

 

00:17:22 does not mean 17 minutes and 22 seconds to a computer, it just means the string "00:17:22".

 

So what's happening is that PHP is casting the string to an int and the string is being casted to 0.

 

 

You'll need to do the math in seconds.  If you already have the time in seconds, then just use those variables instead.  If not, you can just use explode to separate the fields then multiply accordingly.

 

Then as far as the percentage math goes:

 

percent swimming = time swimming / total time * 100;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.