drcsports Posted January 14, 2011 Share Posted January 14, 2011 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? Also in the future i'd like to average out time in this same format on anothe rproject Quote Link to comment https://forums.phpfreaks.com/topic/224467-adding-time-together/ Share on other sites More sharing options...
Pikachu2000 Posted January 14, 2011 Share Posted January 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224467-adding-time-together/#findComment-1159565 Share on other sites More sharing options...
Pikachu2000 Posted January 14, 2011 Share Posted January 14, 2011 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` Quote Link to comment https://forums.phpfreaks.com/topic/224467-adding-time-together/#findComment-1159570 Share on other sites More sharing options...
drcsports Posted January 17, 2011 Author Share Posted January 17, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/224467-adding-time-together/#findComment-1160732 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.