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
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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.