gscharlemann Posted October 30, 2009 Share Posted October 30, 2009 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted October 30, 2009 Share Posted October 30, 2009 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; Quote Link to comment Share on other sites More sharing options...
gscharlemann Posted October 30, 2009 Author Share Posted October 30, 2009 10-4. Thanks Corbin. I'll give that a shot. greg Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 30, 2009 Share Posted October 30, 2009 if you're grabbing this information from a database, you can use some of MySQL's native functions to help you out and unburden PHP a bit. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.