Page 1 of 1

Add time(hh:mm:ss) to time(hh:mm:ss)

Posted: Thu Sep 03, 2009 4:31 pm
by tapaway
In an online course I operate I am trying to track a user's totalTime spent taking the course over multiple user sessions.

I can calculate the totalTime a user has spent during first session by subtracting timeStarted from timeEnded using PHP. I am able to get a totalTime in hh:mm:ss.
This time I can store in MySQL.

My problem arises when i try to add the time from first session (hh:mm:ss) to a total time from a second session (hh:mm:ss) using PHP. The answer will allow me to track cumulative time spent over multiple sessions.

Any suggestions of how to add time(hh:mm:ss) to time(hh:mm:ss) in PHP?

Any input is greatly appreciated.
Sean

Re: Add time(hh:mm:ss) to time(hh:mm:ss)

Posted: Thu Sep 03, 2009 4:44 pm
by requinix
It'd be a lot easier if you tracked the time spent on the course in seconds. Then when you want to display it you do a bit of math.

Re: Add time(hh:mm:ss) to time(hh:mm:ss)

Posted: Thu Sep 03, 2009 6:22 pm
by flying_circus
tapaway wrote:Any suggestions of how to add time(hh:mm:ss) to time(hh:mm:ss) in PHP?
Sean,

Basically what Tasairis said is the easist way. Store your time in a timestamp. A timestamp is defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds.

strtotime() takes a string and converts it to a timestamp. It understands ALOT of variations of time formats.

Code: Select all

<?php
  $accrued_time = strtotime("10:38:46");
  $current_session = strtotime("2:28:39") - strtotime("00:00:00");
  print date("H:i:s", ($accrued_time + $current_session));
  // Prints: 13:07:25 
?>
However, this is an interesting problem if you want to accrue time in the format that you currently are. For example, lets say I've spent 450 or so hours on your site. If you continue to store data in that format, the above code will not work. All time based functions seem to be on a 24 hour period only. As soon as you go to a tripple digit for the hour, it seems to fail.

I played with it a bit and came up for a text based solution, but I think its terrible and prone to errors.

Code: Select all

<?php
  $accrued_time = explode(":", "10:38:46");
  $current_session = explode(":", "2:28:39");
  // Total Time: 13:07:25
  
  if(count($accrued_time) == count($current_session)) {
  # Define Variable for New Time
    $new_time = array();
    
  # Add it up!
    for($i=0; $i<count($accrued_time); $i++) {
      $new_time[$i] = $accrued_time[$i] + $current_session[$i];
    }
    
  # Carry time if minutes or seconds are greater than 59
    for($i=(count($new_time) - 1); $i>=0; $i--) {
      if($new_time[$i] >= 60) {
        $new_time[$i - 1] += intval($new_time[$i] / 60);
        $new_time[$i] = $new_time[$i] % 60;
      }
    }
    
  # Display Results
    printf("%d:%02d:%02d", $new_time[0], $new_time[1], $new_time[2]);
    // Prints: 13:07:25 
  }
?>
Do with it what you will, but I think storing your time in a timestamp is the best. You can store it as an int in SQL or a timestamp which I believe is datetime type. Then you can use the date() function to easily spit out like 7 day, 24 hours, 6 mins, 19 sec, etc.

Re: Add time(hh:mm:ss) to time(hh:mm:ss)

Posted: Thu Sep 03, 2009 6:44 pm
by tapaway
Thank you both. I will give it a go.

There are many scripts to calculate a difference between 2 times but not to sum them.

Thanks again.

Sean