Jump to content

Dividing time


davidcriniti

Recommended Posts

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>

Link to comment
Share on other sites

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

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.