Skip to main content
Participating Frequently
September 4, 2012
Question

Add together times

  • September 4, 2012
  • 3 replies
  • 1338 views

I am wanting to add together a series of stopwatch times, hours mins secs eg 00:10:22 + 01:44:03 = 01:54:25 etc there could be two or 3 or more. They are saved in mySQL as time.

Is this possable? Can be saved as different format if needed.

Cheers

    This topic has been closed for replies.

    3 replies

    headtoesAuthor
    Participating Frequently
    September 9, 2012

    in the end did it by making two queries, one the usual and one using group by and sum, then did a query of queries using both of them

    one small detail I havent worked out is how to take the seconds back to time format, at the moment Im using

    <cfset Time = '#int((seconds/60)/60)#:' & '#(seconds/60) MOD 60#:' & '#seconds MOD 60#'>

    this works fine but if there is a single digit it only displays one eg 1:1:1 whereas I would prefer to have 01:01:01

    thanks for your help, wouldnt have been able to do it otherwise

    headtoesAuthor
    Participating Frequently
    September 7, 2012

    thanks for the replies, I have a slightly differnt problem now. I am grouping the times by the days they are made on. I can output the times seperately and they group by days ok but I cant think of a way to add them together. the code below gives me the total of all the times for all days and seems to ignore the grouping even tho it is within the cfoutput grouping tag. getting into and out of seconds seems no problem but Im out of ideas

    <cfquery name="times" datasource="langDB">

        SELECT date(UserQuiz.dateCompleted) as newdatecompleted,  TIME_TO_SEC(userquiz.timespent) AS timespent

        FROM  userquiz

         order by newdatecompleted desc

    </cfquery>

    <cfoutput query="times" group="newdateCompleted">

    <cfoutput> #times.timespent# </cfoutput>

    <cfset totaltime = arraySum(times['timespent'])>

      #totaltime#

    </cfoutput>

    Inspiring
    September 7, 2012

    Look at the sql sum() function and group by clause.

    headtoesAuthor
    Participating Frequently
    September 7, 2012

    thanks, I was trying sql sum initialy but it was only bringing up one result, I couldnt work out why. I didnt realize there was a group by in sql. I guess if I use it there I dont need it in cfoutput? Anyway, will play around and see what results I can get

    Inspiring
    September 4, 2012

    I think you are better off storing the number of seconds for each record.  The time datatype is more appropriate for the time of day.

    WolfShade
    Legend
    September 4, 2012

    Check cflib.org.. there might be a CFC that someone made that will do what you are looking for.

    ^_^