davidcriniti Posted December 31, 2014 Share Posted December 31, 2014 I have a table where I'm storing my marathon and other race results. One column ( race_time_total - TIME format) and another column ( race_distance_numeric - decimal(65,3) format) are being used here. Assuming the race_distance_numeric is the distance of the race in either km or miles (Eg: marathon being 42.195km), how to I calculate the pace? I've used: $pace_per_k = $row_content['race_time_total'] / $row_content['race_distance_numeric'] .... but am not getting expected results when I echo $pace_per_k Any tips? Quote Link to comment Share on other sites More sharing options...
CroNiX Posted December 31, 2014 Share Posted December 31, 2014 (edited) MySQL TIME format is HHH:MM:SS, so how do you divide that by a number and expect a sane result with colons in there? Edited December 31, 2014 by CroNiX Quote Link to comment Share on other sites More sharing options...
CroNiX Posted December 31, 2014 Share Posted December 31, 2014 You might want to use the TIME_TO_SEC(race_time_total) function in your query so that it converts hours/minutes/seconds into just seconds, and then divide. Then format your result back into H:M:S to display. That can actually all be done within the query without needing PHP. Quote Link to comment Share on other sites More sharing options...
Zane Posted December 31, 2014 Share Posted December 31, 2014 Speed is measured in meters per second; you will have to convert otherwise. You will also have to reverse your formula. speed = distance (meters) / time (seconds) CroNix is correct. This can all be calculated within the query, but you cannot subtract time without using the built-in MySQL functions for doing so. http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html In your case, i would go with cronix' idea of using TIME_TO_SEC() 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.