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. Link to comment https://forums.phpfreaks.com/topic/179570-time-math-calculate-percentage-of-time/ 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; Link to comment https://forums.phpfreaks.com/topic/179570-time-math-calculate-percentage-of-time/#findComment-947578 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 Link to comment https://forums.phpfreaks.com/topic/179570-time-math-calculate-percentage-of-time/#findComment-947804 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. Link to comment https://forums.phpfreaks.com/topic/179570-time-math-calculate-percentage-of-time/#findComment-947822 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.