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
https://forums.phpfreaks.com/topic/255546-dividing-time/
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
https://forums.phpfreaks.com/topic/255546-dividing-time/#findComment-1310124
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.