Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Calculating Time Worked 1

Status
Not open for further replies.
Mar 23, 2015
37
US
Hello!

My on again, off again database project is on again with new bosses. Now they would like to track time worked. I have a table with date, time in and time out. I need to calculate the hours:minutes worked and show them in the table:

Code:
 			<tr>
     			<td><?php echo date('m/d/Y', strtotime($row['date']))?></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 = $t_out - $t_in ; echo ($row['t_total'])?></td> 
   			</tr>

The calculation line is blank so I need to figure the correct syntax for that.
The next step will be to add up all the rows and calculate the amount of time worked year to date.

Thanks for your help in advanced.
 
I think I'm making progress. I've change the calculation to:

Code:
<?php $t_total = strtotime('$t_out') - strtotime('$t_in') ; echo $t_total?>

This gives me a value, but with $t_in = 12:00 PM and $t_out = 1:05 PM I'm getting a value of 0. Better than a blank... but still not correct.
 
Hi

Sure you are not mixing $t_out / $t_in / $t_total variables and $row['t_out'] / $row['t_in'] / $row['t_total'] array items ?

I expected to see something like this instead :
PHP:
[b]<td>[/b][teal]<?php[/teal] [navy]$t_total[/navy] [teal]=[/teal] [navy]$row[/navy][teal][[/teal][i][green]'t_out'[/green][/i][teal]] -[/teal] [navy]$row[/navy][teal][[/teal][i][green]'t_in'[/green][/i][teal]] ;[/teal] [b]echo[/b] [navy]$t_total[/navy][teal]?>[/teal][b]</td>[/b]

Feherke.
feherke.ga
 
Hi feherke,

Yes, that's better.
Code:
<td><?php $t_total = $row['t_out'] - $row['t_in'] ; echo $t_total?></td>

I'm now returning a value of 1 in the table row. I should have 1.08. I'm much, much closer than I was a minute ago.
Thanks!
 
I'm now formatting correctly, but I'm still fine tuning the calculation -

Code:
<td><?php $t_total = $row['t_out'] - $row['t_in'] ; echo number_format((float)$t_total, 2, '.', '');?></td>
 
Got the first part working!

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

This returns the value of 1:05 as it should.

Thanks for the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top