Need Help Calculating Results

I have a database where I track time worked. The database has date, t_in & t_out and I return that in a web table. In the table I calculate the total time worked:

<td><?php $t_total = strtotime($row['t_out']) - strtotime($row['t_in']) ; echo gmdate('H:i', $t_total);?></td>

In the table footer I’d like to show the total time worked for the current month and the total time worked year to date.

<tr><td colspan='6'><span>Total time worked in <?php echo date('F')?>:&nbsp;<?php echo gmdate('H:i', $timeMonth);?></span></td></td>
<tr><td colspan='6'><span>Total time worked Year to Date:&nbsp;<?php echo gmdate('H:i', $timeYear);?></span></td></td>

I just don’t know how or where to do the calculation so I can call it in the table footer. I assume it needs to be done in the table, but nothing I’ve tried returns any results…

Here is the complete code:

 <?php 
 	$id = $_GET['id'];
	$sql = "SELECT * FROM timetrack WHERE vol_id = '$id' ORDER BY date, t_in";
   	$result = mysql_query($sql) or die(mysql_error());
?>
  
 <h4 style="margin-left:25px;">Dates and Times Worked</h4>

<table >
	<thead>
    		<tr>
			<th></th>
        		<th>Date</th>
        		<th>Department</th>
        		<th>Time In</th>
        		<th>Time Out</th>
			<th>Total Time</th>
		</tr>
  	</thead>
 	<tbody>
		<?php 
        		while($row = mysql_fetch_array($result))
  				{  
   			?>

 		<tr>
                    <td><a href=editTime.php?id=<?php echo $row['id'];?>>Edit</a></td>
	            <td><?php echo date('m/d/Y', strtotime($row['date']))?></td>
		    <td><?php echo($row['department'])?></td>
     		    <td><?php echo date('g:i A', strtotime($row['t_in']))?></td>
     		   <td><?php echo date('g:i A', strtotime($row['t_out']))?></td>
     		   <td><?php $t_total = strtotime($row['t_out']) - strtotime($row['t_in']) ; echo gmdate('H:i', $t_total);?></td>
     		</tr>
  	</tbody>
      	<?php 
  	    }
  	?> 	 
       <tfoot>	
               <tr><td colspan='6'><span>Total time worked in <?php echo date('F')?>:&nbsp;<?php echo gmdate('H:i', $timeMonth);?></span></td></td>
              <tr><td colspan='6'><span>Total time worked Year to Date:&nbsp;<?php echo gmdate('H:i', $timeYear);?></span></td></td>
      </tfoot>
		
</table>

Thanks!

It should be done in the database.

You also shouldn’t be using the old mysql_ functions.

Thanks.

What are the column types for the t_in and t_out?

You want want to do the math there and alias the new column for the returned result set.

Thanks!

What are the column types for the t_in and t_out?

t_in and t_out are time inputs. Example 08:55 AM.

You want want to do the math there...

Okay. I have the math there that calculates the total time and shows it… hmmmm it’s as if I’m almost there.

and alias the new column for the returned result set.

I’m sorry I don’t quite follow. I’m working on it though.

I’m making progress. I’ve gotten an accurate total time worked to show in the footer.

<?php $newTime = $timeTotal + $newTime; ?>

I just need to figure out the proper calculations for current month and year to date.

Thanks!!

I was referring to having the database do the math and output the difference…

I see. Thanks again.

Sponsor our Newsletter | Privacy Policy | Terms of Service