Adding together time hh:mm:ss to a total

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lofty
    New Member
    • May 2007
    • 1

    Adding together time hh:mm:ss to a total

    Hi,

    I'm building a time report system for work and I want to show the total hours and minutes a project have taken but can't get it to work ...

    I have a database (MySQL) in one table I have the date in one colum and the time in one colum. The date i formated like this YYYY-MM-DD and set to date. And the TIME is like this HH:mm:ss.
    My code looks like this right now...

    [PHP]<?php
    $sql="select
    p.id, p.date, p.time
    from rap_time p
    order by 1 desc";
    $rows = mysql_query($sq l);
    $totaltime = 0;
    while($row = mysql_fetch_arr ay($rows)) {

    echo"$row[1]";
    echo", ";
    echo"$row[2]";
    echo"<br />";
    $totaltime = $totaltime + $row[2];
    }
    echo"$totaltime ";
    ?>
    [/PHP]
    The $totaltime just shows the total hours and it does not count the hours and minutes together.
    So if I have an output of times that looks like this:
    01:10:00
    01:13:00
    01:40:00
    the totaltime will show:
    3
    Not 04:03:00
    And yes I'm a noob on PHP and SQL...
  • Lick
    New Member
    • May 2007
    • 3

    #2
    You will have to write code that first counts the seconds and see if they overflow the 59 seconds limit. If they overflow, you add 1 to the minute calculation.

    Next, you count the minutes and see if they overflow the 59 minutes limit. If they overflow, you add 1 to the hours calculation.
    Note: don't forget to add the 1 from the previous calculation, if applicable.

    Next, you count the hours and if applicable, add 1 from the previous calculation.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Here's how I would do it:

      Assuming the times don't span multiple days, pick a random day. For example, '2007-05-14' (to pick a random one). Then prepend the date to your times and calculate their unix timestamps.

      Then, all you have to do is add up the differences between your times and midnight of the same day:

      [code=php]
      var $date = '2007-05-14';
      var $datetime = strtotime("$dat e 00:00:00");

      // These are examples; you'll probably want to pull your numbers from the DB.
      var $times = array(
      '01:15:45',
      '03:11:02',
      '10:15:45'
      );

      var $sum = 0;
      foreach($times as $time) {
      $sum += strtotime("$dat e $time") - $datetime;
      }

      // $sum holds the total number of seconds of all three times.
      print($sum);
      [/code]

      When the loop executes the first time, we are computing the difference between two datetimes:

      [code=php]
      $sum += strtotime('2007-05-14 01:15:45') - strtotime('2007-05-14 00:00:00');
      [/code]

      You're taking the number of seconds from January 1, 1979 (or something like that) until May 14, 2007 at 1:15 AM and 45 seconds. Then you take the number of seconds from January 1, 1979 until May 14, 2007 at midnight and subtract the two numbers.

      Since May 14, 2007 01:15:45 occurred exactly 1 hour, 15 minutes and 45 seconds after May 14, 2007 00:00:00, $sum has just been +='ed to the number of seconds in 1 hour, 15 minutes and 45 seconds (4515).

      Continue through the loop with the rest of your values, and you've got yourself a handy little time-summer.

      Now the trick comes into play when your times span >1 day. But the fix for that isn't too difficult ;)

      Comment

      • skillednerd
        New Member
        • Jul 2007
        • 1

        #4
        [code=php]list($h, $m, $s) = split('[:]', $time_1_here);
        list($h2, $m2, $s2) = split('[:]', $time_2_here);

        $h = ($h * 60) * 60;
        $m = $m * 60;
        $h2 = ($h2 * 60) * 60;
        $m2 = $m2 * 60;


        $x = $h + $m + $s;
        $x2 = $h2 + $m2 + $s2;

        $y = $x + $x2;

        $y = $y / 60;
        $y = $y / 60;

        echo $y;[/code]

        [Please use CODE tags when posting source code. Thanks! --pbmods]

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          You know, the reason timestamps were ever used is because you can calculate the difference between them with simple subtraction. You can use mktime() to turn the dates into timestamps, and then do this:

          [code=php]$end - $start = $timeTaken;
          $years = date('Y', $timeTaken) - 1970; // because timestamps start at 1970
          $months = date('n', $timeTaken);
          $days = date('j', $timeTaken);[/code]

          Comment

          • helalreza
            New Member
            • Apr 2016
            • 9

            #6
            Code:
            <body>
            <input type="time" step="1" id="startTimeInput"  name="start_time" value=""><br>     <!--   write as like       02:31:55   -->
            <input  type="time" step="1" id="endTimeInput"  name="end_time" value=""><br>
            <button id="calcBtn">Seconds</button><br>
            <div id="result"></div>
            <script type="text/javascript" src="js/jquery.js"></script>
            <script type="text/javascript">
            document.getElementById('calcBtn').onclick = function(){
            	var startTime = document.getElementById('startTimeInput').value;
            	var endTime = document.getElementById('endTimeInput').value;
              var result = getDeltaSeconds(startTime, endTime);
              document.getElementById('result').innerHTML = result + ' sec';
            }
            function getDeltaSeconds(startTime, endTime){
            	var startTimeSeconds = getSeconds(startTime);
              var endTimeSeconds = getSeconds(endTime);
              return endTimeSeconds - startTimeSeconds;	
            }
            function getSeconds(timeString){
            	var parts = timeString.split(' ');
              var time = parts[0];
              var ampm = parts[1];
              var timeParts = time.split(':');
              var seconds = Number(timeParts[2]) + Number(timeParts[1]*60) + Number(timeParts[0]*60*60);
              return ampm == 'AM' ? seconds : seconds + 12*60*60;
            }
            </script>
            </body>

            Comment

            Working...