×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Time Difference Calculantion

Time Difference Calculantion

(OP)
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!!!

RE: Time Difference Calculantion

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;

/* 15 */ 

----------------------------------
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

RE: Time Difference Calculantion

(OP)
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 RE: Time Difference Calculantion 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

RE: Time Difference Calculantion

(OP)
vacunita,
sorry for the slow reply, thank you!!! that worked!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!