Jump to content

Adding Time together


drcsports

Recommended Posts

I want to code the function that will pull data from my Database and add it together for timing purposes.

 

+------------------------------------------------------------+   

|    Format = date('H:i:s')

|    i.e. 01:15:25 + 01:51:11 = 03:06:36

|   

|    $timeA = 01:15:25;

|    $timeB = 01:51:11;

|   

|    result = $timeA+$timeB = (03:06:36)

+------------------------------------------------------------+   

 

 

This is where i've gotten to:

------------------------------------------------------------------------------------

$sql = "SELECT * FROM `results` WHERE  id = '1'";

$res = mysql_query($sql) or die(mysql_error());

$time1 = mysql_fetch_assoc($res);

 

$sql = "SELECT * FROM `results` WHERE  id = '2'";

$res = mysql_query($sql) or die(mysql_error());

$time2 = mysql_fetch_assoc($res);

 

echo $time1['finish_time']+$time2['finish_time'] . '<br />';

-------------------------------------------------------------------------------------

But end up with a 2? :wtf:

 

Also in the future i'd like to average out time in this same format on anothe rproject

 

 

Link to comment
https://forums.phpfreaks.com/topic/224467-adding-time-together/
Share on other sites

You can do the addition right in the query string:

SELECT ADDTIME( `time`, (SELECT `time` FROM 1`table` WHERE `id` = 1) ) AS `total_time` FROM `table` WHERE `id` = 2

 

With both values as 01:10:10, returns:

total_time

02:20:40

Note that in the example above and the one that follows, I'm assuming the times in the database are durations, and not times of the day.

 

For averaging times, this works. There may be a better way, but it didn't jump out at me.

SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(`time`))) AS `avg_time` FROM `table`

 

Thanks. This will be helpful. It is going to be searching through a table of thousands of results and adding three 'durations' of the same individual for a specific event.

 

The average will be hel;pful in another project down the road so thanks for the input...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.