Announcement

Collapse
No announcement yet.

Calculating + Adding Times

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculating + Adding Times

    Ok, I don't know if the title is correct, but I'll give it a try.

    I have 2 questions:
    I have stored 2 Dates in one row in one table. For instance:
    +-------+--------+
    | 12:00 | 13:15 |
    +-------+--------+

    Now I want to calculate the difference between these two times. In the example above the difference would be 1 hour and 15 Minutes. I want it to display like "1,25". How do I calculate this? If I have (e.g) 20 rows and I want to calculate and display 20 differences.

    My second question is: How do I calculate the 20 differences? Like this:
    1,25 ; 2,30, 1,45; 7,15; and so on. And I want the sum of all the 20 differences. I hope you understand what I mean.
    Like 1.25 + 2.30 + 1.45 + 7.15 etc.

    I'd appreciate any help.

  • #2
    I don't understand the '20 differences' bit, so I'll skip it.

    For the first, you can do something like this...
    PHP Code:
    $result mysql_fetch_array(mysql_query($sql));
    $start strtotime($result['start_bit']);
    $end strtotime($result['end_bit']);
    $diff = ($end $start) / 60 60;
    echo 
    "Difference ".number_format($diff,2)." <br>"
    Lats...

    Comment


    • #3
      doesn't work :/
      The "Time1" and "Time2" in the DB are like 00:00:00 (MySQL-Type: TIME)

      *edit*: Figured out why it didn't work. Thank you

      Comment


      • #4
        No one can help me with adding all the timediffs?

        I mean, when I have calculated all the timediffs, which are stored in the DB, I want the sum of all timediffs displayed in the bottom.
        Like:

        +--------------------------+
        | Time 1 | Time 2 | Timediff |
        +--------------------------+
        | 12:00 | 13:00 | 1, 00 |
        +------------------------- +
        | 15:00 | 16:00 | 1,00 |
        +--------------------------+
        SUM: 2,00

        ^--like this above. Ok, In the example 2 variables aren't that much, so I can write $sum = $time1 + $time2 - but when I have more than 50 times stored in the DB...

        So, anyone can help?

        Comment


        • #5
          Something like this...
          PHP Code:
          $sql_result mysql_query($sql);
          while(
          $result mysql_fetch_array($sql_result))
          {
           
          $start strtotime($result['start_bit']);
           
          $end strtotime($result['end_bit']);
           echo 
          "$start - $end - ";
           
          $diff = ($end $start) / 60 60;
           echo 
          "Difference ".number_format($diff,2)." <br>";
           
          $total_diff += $diff;
          }
          echo 
          "Total ".number_format($total_diff,2); 
          Lats...

          Comment


          • #6
            Thanks, dude, works excellent

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...
            X