close ad
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues


Calculate Difference in Time with PHP

Thread began 3/12/2010 10:57 am by tom92909 | Last modified 3/15/2010 1:47 pm by Justin Nemeth | 2601 views | 2 replies |

tom92909Beta Tester

Calculate Difference in Time with PHP

Howdy folks! :)

I've been working on a new project that calculates hours worked. I need to take up to three 24hr clock IN and clock OUT entries stored in MySQL and come up with a calculated sum difference in time.

I've seen lots of examples from folks via Google searches, but I'm stuck. I can't seem to get the the calculations right.

I am storing up to a possible 3 entries per/work day. Below is my weak attempt to get started...

$query = "SELECT * FROM emphours

        WHERE `Date` BETWEEN '".$_POST['Start']."' and '".$_POST['End']."'";

    $ext1 = "$time1:";

    $ext2 = "$time2:";

    $ext3 = "$time3:";

    $ExtTot = $ext1 + $ext2 + $ext2;

I found a web tool that is an outstanding example of what I'm trying to accomplish, except I need to make some modifications to suit my exact requirements... I'm attempting to contact the author/website owner to gain some insight.


The scripting this site has is right on target.

Any tips, pointers, suggestions would be greatly appreciated.


Sign in to reply to this post

Jason ByrnesWebAssist

you could do this all in your query.

the sql function for subtracting two times is TIMEDIFF, for adding two times ADDTIME

heres an example query using real time values:

SELECT TIMEDIFF('2010-12-24 12:13:44','2010-12-24 04:15:44') as time1, TIMEDIFF('2010-12-22 11:13:44','2010-12-22 02:15:44') as time2, TIMEDIFF('2010-12-20 9:13:44','2010-12-20 5:15:44') as time3, ADDTIME(TIMEDIFF('2010-12-24 12:13:44','2010-12-24 04:15:44') ,ADDTIME(TIMEDIFF('2010-12-22 11:13:44','2010-12-22 02:15:44'), TIMEDIFF('2010-12-20 9:13:44','2010-12-20 5:15:44'))) AS totalHours;

or to use the columns in your DB:
SELECT TIMEDIFF(Out1, In1) as time1, TIMEDIFF(Out2, In2) as time2, TIMEDIFF(Out3, In3) as time3, TIMEADD(TIMEDIFF(Out1, In1) ,TIMEADD(TIMEDIFF(Out2, In2), TIMEDIFF(Out3, In3))) AS totalHours

Sign in to reply to this post

Justin Nemeth

A cool PHP function to look into is strtotime(). Basically you can manipulate a date/time string pretty easily.


Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question





Ease of use


security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...