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!

Time Difference Calculantion 1

Status
Not open for further replies.

evr72

MIS
Dec 8, 2009
265
US
Hello,

I am very new to programming and PHP, I have 3 fields "TIMIN" captures the log in time "TIMOUT" captures the time out
and "TOTTIME" is supposed to get the difference of "TIMIN" - "TIMOUT"

the way the time fields were created is a bit restricting they are only 6 digits so I have to get the time as a number,
for example for 10:55 AM the way I pass the value to the database is 1055
I have seen a few examples where the time out is the time in is substracted from the time out and then divided by 60
this does not work, let's say an employee logs in at 1055 and logs out at 1110 the "TOTTIME" sould be .15
if I do the calculation the total time 1110 - 1055 = 55 / 60 = .9166

Any help is much appreciated.

thanks!!!

 
You can use PHP's strtotime() function to turn the times into actual timestamps and then be able to make calculations of off them.

Code:
$timein = "1055";
$timeout = "1110";

$timeok1 = strtotime($timein);
$timeok2 = strtotime($timeout);
$timetot = $timeok2 - $timeok1;

echo $timetot / 60; 

[COLOR=#4E9A06]/* 15 */[/color]






----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
vacunita,
for some reason the code did not work I had some issues because the time being capturd is based on 24 hr

I found the code below and it works well, the only issue I have is that it gives me a whole number

Time in: 5:58 AM
Time Out: 9:00 AM

With the code below this is what I get
TIMIN = 558
TIMOUT = 900
TOTTIME = 302
Should be =3.02


Code:
$TIMEIN = $values["TIMEIN"];
$TIMOUT = $values["TIMOUT"];

$TIMEIN = substr($TIMEIN, 0, strlen($TIMEIN)-2 ) . ":" . substr($TIMEIN, -2); $TIMOUT = substr($TIMOUT, 0, strlen($TIMOUT)-2 ) . ":" . substr($TIMOUT, -2);
$time1 = strtotime($TIMEIN);
$time2 = strtotime($TIMOUT);
$t=$time2 - $time1;
if ($t < 3600)  
	$values["TOTTIM"] = sprintf('.%02d', ($t/60%60)); else
	$values["TOTTIM"] = sprintf('%d%02d', ($t/3600),($t/60%60));

I have tried to ticker with the code but not having any luck

any help is much appreciated
 
1. You don't need to remove the colon, or the am/pm identifier. strtotime is able to interpret that directly. Your substr() routines are adding colons rather than removing them anyway so it produces an invalid time string. Which makes everything else fail.

2. not sure what your if statement is trying to do there. If you need to output the result in a time format you can use the date() function to directly do that.

Code:
$TIMEIN = "10:45 am";
$TIMOUT = "12:25 am";

$time1 = strtotime($TIMEIN);
$time2 = strtotime($TIMOUT);
$t=$time2 - $time1;
$values["TOTTIM"] = date("h:i",$t);

echo "<br><pre>" . print_r($values,1) . "</pre>";

That code should output

Array
(
[TOTTIM] => 02:40
)

for the time difference for those values. You can try it out with other values, and verify it works.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
vacunita,
sorry for the slow reply, thank you!!! that worked!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top