close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Calculate Difference in Time with PHP

Thread began 3/12/2010 1:57 pm by tom92909 | Last modified 3/15/2010 4:47 pm by Justin Nemeth | 1714 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...

php:
$query = "SELECT * FROM emphours

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

    $time1=$row["Out1"]-$row["In1"]; 
    $ext1 = "$time1:";

    $time2=$row["Out2"]-$row["In2"]; 
    $ext2 = "$time2:";

    $time3=$row["Out3"]-$row["In3"]; 
    $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.

timesheet.php

The scripting this site has is right on target.

Any tips, pointers, suggestions would be greatly appreciated.


Tom

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.

strtotime

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

Content

rating

Layout

rating

Ease of use

rating

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