Doing a calculation in a while loop
<p>I have the following variables inside a WHILE loop:</p>

<pre><code>    $row['To'];
    $row['From'];
</code></pre>

<p>Those represent a time, example:</p>

<pre><code>$row['To'] ="10:00:00";
 $row['From'] = "08:00:00";
</code></pre>

<p>In a normal calculation I will do the following to get the hours in difference:</p>

<pre><code> $result = (strtotime($row['To']) - strtotime($row['From']));
   $hours      = floor($result / 60 / 60);
   $minutes    = round(($result - ($hours * 60 * 60)) / 60);
</code></pre>

<p>The problem is that now I have to do in a while loop, where $row['to'] and $row['From'] must be calculated all the times the loop dictate...But I cannot get it work. This is what I tried:</p>

<pre><code>function calculateHours($project, $worker, $year, $month) {
if($project) {
$q='SELECT * FROM project_timeline WHERE ID="'.$project.'" AND Year(Date) = "'.$year.'" AND Month(Date) = "'.$month.'" AND WorkerID="'.$worker.'"';
 }else{
$q='SELECT * FROM project_timeline WHERE Year(Date) = "'.$year.'" AND Month(Date) = "'.$month.'" AND WorkerID="'.$worker.'"';
 }
 $r=mysql_query($q) or die(mysql_error());   
   while($row = mysql_fetch_array($r)) {
    $result .= (strtotime($row['To']) - strtotime($row['From']));
   }
    $hours      = floor($result / 60 / 60);
    $minutes    = round(($result - ($hours * 60 * 60)) / 60);
 return $hours.' hour(s) and '.$minutes.' minutes';
}
</code></pre>

<p>How can I fix it?
Thanks</p>
