davidcriniti Posted January 22, 2012 Share Posted January 22, 2012 Hi, I'm trying to make a page that records the training I do. I've got a column for time (total time for the whole training session), and one for laps (how many laps completed). I've got another for average lap time, which I've calculated as $avg_lap_time = $time / $laps; Everything is displaying fine except the average lap time, which is just displaying as 0. Any idea how to fix this? More of the code is below. <table class="tenk"> <tr><th>*</th><th>Date</th><th>Course</th><th>Course Distance</th><th>Laps</th><th>Time </th><th>Average lap time </th></tr> <?php $result = @mysql_query($select . $from . $where); if (!$result) { echo '</table>'; exit('<p>Error retrieving info from database!<br />'. 'Error: ' . mysql_error() . '</p>'); } $row_counter = 1; //create row counter with default value 0 while ($results = mysql_fetch_array($result)) { echo "<tr valign='top'>\n"; $training_date = $results['training_date']; $course_name = $results['course_name']; $course_distance = $results['course_distance']; $laps = $results['laps']; $time = $results['time']; $avg_lap_time = $time / $laps; echo "<td>"; echo $row_counter++; echo "</td>\n"; echo "<td>$training_date</td>\n"; echo "<td>$course_name</td>\n"; echo "<td>$course_distance</td>\n"; echo "<td>$laps</td>\n"; echo "<td>$time</td>\n"; echo "<td> $avg_lap_time </td>\n"; echo "</tr>\n"; } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/255546-dividing-time/ Share on other sites More sharing options...
Pikachu2000 Posted January 22, 2012 Share Posted January 22, 2012 What format is the time field? Quote Link to comment https://forums.phpfreaks.com/topic/255546-dividing-time/#findComment-1310116 Share on other sites More sharing options...
davidcriniti Posted January 22, 2012 Author Share Posted January 22, 2012 The time field is in time format and the laps field is in int format. Quote Link to comment https://forums.phpfreaks.com/topic/255546-dividing-time/#findComment-1310120 Share on other sites More sharing options...
Pikachu2000 Posted January 22, 2012 Share Posted January 22, 2012 So you're trying to divide, let's say 1:20:43 by 25? For obvious reasons, that won't work. What you need to do is convert the time to second, divide, convert back to HH:MM:SS format. You can do that easily enough in the query string. This would make the value available in the $results['lap_avg'] element. SELECT SEC_TO_TIME(TIME_TO_SEC(time) / laps) AS lap_avg Quote Link to comment https://forums.phpfreaks.com/topic/255546-dividing-time/#findComment-1310124 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.