Question : Best Way To Subtract DateTime Fields

I am working on a small php application that will handle work-order requests.  When a user creates a work-order, I am storing the time of the request into a datetime field named "RequestDateTime".  When an admin takes control of the case, I am storing that in a datatime field named "ClaimDateTime".  When the admin closes the case, I am storing that  in a datetime field named "ClosedDateTime".  I would like to store the difference between the RequestDateTime value and ClaimDateTime value into a field named "ResponseTime".  Here's an example:
If the RequestDateTime value is "2010-07-22 10:24:12", and the ClaimDateTime value is "2010-07-22 11:25:13", I would like to populate the ResponseTime filed with something similar to this: "0 days, 1 hour, 1 minute, 1 second".  The formatting of the ResponseTime filed is actually part of my question.  I don't know the best way to store the difference of the two date/time values.  Should I make fields for each value? Like "ResponseTimeDay", "ResponseTimeHour" etc..., or should I leave it as one filed named "ResponseTime" and populate it similarly to the above example?  Eventually I will want to calculate an average response time, so I'm looking for whatever format helps me achieve that with no hassle.  I will use this same technique to populate the "ClosedDateTime" field.  Now that you know a little about the system, here are my questions:

How do I subtract DateTime fields?
Do I need to store the subtracted values into a field in the database, or should I simply recalculate every time I wish to see the value?
If I'm parsing through 40 cases, how can I calculate an average response time?  

Thanks for reading. Please let me know if I need to provide any additional info.  

Answer : Best Way To Subtract DateTime Fields

Hi,

It is a calculated value, I wouldn't store it, just recalculate as needed. You could return it as a field from your database as

Select mydate1, mydate2,TIMEDIFF(mydate1,mydate2) as diff from mytable

this will return your 2 dates and a timevalue called diff

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timediff
Random Solutions  
 
programming4us programming4us