1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Adding Time Using PHP ...

Discussion in 'Web Design & Development' started by edjon2000, Apr 17, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. edjon2000

    edjon2000 Thread Starter

    Joined:
    Jul 16, 2004
    Messages:
    279
    First Name:
    Jon
    Hi all,

    Sorry it's been so long since I last posted,

    I am trying to use a function to add up a series of hours for my little timesheet project, I know that to calculate the difference between 2 times to produce a result of hours, minutes and seconds for say a working morning eg 0900 to 1225 is 3 hours 25 minutes can be achieved relatively easily using the PHP strtotime() function but I am struggling to add up the total hours for the working week, I thought I could just use a variation on the strtotime() function but it is not that easy to do, maybe, I am missing out something obvious.

    Basically the worked hours for the morning and for the afternoon are stored in a MySQL database as follows

    Day, Monday am start time 0900 end time 1200 hours 3:00:00
    Day, Monday pm start time 1230 end time 1605 hours 3:35:00

    and so on throught the week.

    I would like to be able to add up the total hours, minutes and seconds for the week and display them on the page.

    so far this is what I have, (I have left in some debugging code and left off the return from the function for the moment) so please disregard the lines starting with #

    PHP:
    function get_tot_hours($array) {
    #    show_array($array);#exit();
        
    foreach($array as $key => $val) {
            echo 
    "\$key = $key and \$val = {$val['hours']}<br />";
            
    $decode strtotime("{$val['hours']}");
    #        $decode = strtotime("00:00:00");
            
    echo "\$decode = $decode<br />";#exit();
            
    $htot += date("h:i:s"$decode);
        }
            echo 
    "\$htot = $htot<br />";
    #        exit();
    }
    Any help would be greatly appreciated, if you require any additional information please feel free to ask

    edit ... I have made a test site available at http://dev.euadvantage.com/art/timesheet.php using the function as it stands, the results are displayed in what would be the total hours results section

    Jon
     
  2. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    I'd just use a bit of standard math and good old MOD. If you have a start and end timestamp you can subtract to get the difference (or time worked) in seconds, then use basic math to work out H / M / S.

    That'd be my call...

    Danny
     
  3. edjon2000

    edjon2000 Thread Starter

    Joined:
    Jul 16, 2004
    Messages:
    279
    First Name:
    Jon
    Hi Danny thanks for the response,

    Yeah to work out the time difference is relatively easy I already do that, my main concern is how to calculate total hours
    from a list of times for the week, that is not so easy, e.g. in my example I would need to calculate the total hours for the week and have it displayed in the total hours section, at the moment it calculates hours only check out the link I mentioned and you will see what I mean, however I do appreciate the response thanks Danny :)

    Any additional help would be welcome

    Jon
     
  4. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    OK Jon, just checked your link and now I'm really confused... You have a list of times worked for each day, and you need to know how to work out the total for the week? Surely you just add them all up, or am I really missing the point here.

    You have a start and end timestamp from which you calculate the hours worked per day, so why not total these differences and convert to H / M / S for the final result, or add a new entry in your DB and keep a running total - every day you add to it and convert it on Friday?

    I think you are over complicating the problem, but if I'm way off base just let me know...

    Danny
     
  5. edjon2000

    edjon2000 Thread Starter

    Joined:
    Jul 16, 2004
    Messages:
    279
    First Name:
    Jon
    Hi Danny,

    I may well be
    At the moment I can retrieve the total times per day for the selected week commencing as you can see in the total hours column but when it comes to adding them up it just adds the hours and doesn't take into account the minutes or seconds, furthermore PHP interprets the value 00:00:00 as the current date at 12:00:00 so adds an additional 12 hours per 00:00:00 entry.

    I will continue to work on the problem however I am sure there must be an easier way :)

    Jon
     
  6. allnodcoms

    allnodcoms

    Joined:
    Jun 30, 2007
    Messages:
    613
    Hi Jon,

    Don't use the formatted times for you interim calculations. Go with raw timestamps and only convert for final output, that will solve all your PHP time formatting issues. Timestamps are just integers holding a number of seconds, and basically that's all you need. Subtracting start_timestamp from end_timestamp gives you the number of seconds worked per day, and I think that's what you should be working with. You can take the PHP time functions totally out of the equation and use simple math on standard integer values, and do all your formatting in the final output.

    That's what I'd do anyway... Good Luck!

    Danny
     
  7. edjon2000

    edjon2000 Thread Starter

    Joined:
    Jul 16, 2004
    Messages:
    279
    First Name:
    Jon
    Hi Danny,

    Ok thanks for your time, I'll let you know how it works out

    Jon
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/917482

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice