473,412 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

How to calculate the difference between 2 timestamps then avg

348 100+
Hi all,

I have a problem getting a correct result with my sql code and figured I would see if anybody could help.

I have two fields in a table that have timestamps. I want to find the difference in time between the two stamps and then calculate the average. Here is the code that I have come up with so far.

Expand|Select|Wrap|Line Numbers
  1. SELECT AVG(TIMEDIFF(departure_Time, arrival_Time)) as average
  2. FROM t1
  3. WHERE
  4. employee_Number= '3'
I have two stamps:

Expand|Select|Wrap|Line Numbers
  1. departure_Time                arrival_Time
  2. 2007-09-23 13:15:29        2007-09-23 13:42:45
  3. 2007-09-23 17:26:41        2007-09-23 17:27:41
  4.  
Based on those timestamps and running the code will return '1408' which I believe is 14 minutes and 8 seconds. I believe that the calculations may be correct but I would either like to have a decimal point or just round out the number to 14. Again, this is the average between the two stamps.

Also, if someone has a better way of doing this, I am open to suggestions.

Thanks guys!

Frank
Sep 23 '07 #1
7 20105
pbmods
5,821 Expert 4TB
Heya, Frank.

The 1408 that you're seeing is 1408 seconds. The average difference is actually 23 minutes and 28 seconds.
Sep 23 '07 #2
fjm
348 100+
Heya, Frank.

The 1408 that you're seeing is 1408 seconds. The average difference is actually 23 minutes and 28 seconds.
Hey Pbmods! I'm glad to see that you are over on this side of the scripts as well. :)

Thanks for the help. Is there a way to convert the seconds to minutes by chance?

Thanks,

Frank
Sep 23 '07 #3
pbmods
5,821 Expert 4TB
Heya, Frank.

Ermmmmmmmmm... divide by 60?

What you probably meant was how to get the minutes and then the remaining seconds. Fortunately, PHP has a modulus operator.
Sep 23 '07 #4
fjm
348 100+
Heya, Frank.

Ermmmmmmmmm... divide by 60?

What you probably meant was how to get the minutes and then the remaining seconds. Fortunately, PHP has a modulus operator.
LOL.. I'm embarrased. :)

Would you recommend that I do the diff and averages in php rather in mysql?

Thanks Pbmods.
Sep 23 '07 #5
pbmods
5,821 Expert 4TB
Heya, Frank.

I'd recommend calculating the averages in MySQL simply because then you are fetching fewer rows from the database. The calculations probably take about the same amount of time on either side, but to run the calculations on the PHP side, you have to first fetch every row for that employee.

Now, if you REALLY wanted to speed things up, you could add a `time_diff` column to your table that stores the result of TIMEDIFF(`departure_Time`, `arrival_Time`):

Expand|Select|Wrap|Line Numbers
  1. INSERT
  2.     INTO
  3.         `t1`
  4.         (
  5.             .
  6.             .
  7.             .
  8.             `departure_Time`,
  9.             `arrival_Time`,
  10.             `time_diff`
  11.         )
  12.     VALUES
  13.         (
  14.             .
  15.             .
  16.             .
  17.             '{$departure_Time}',
  18.             '{$arrival_Time}',
  19.             TIMEDIFF('{$departure_Time}', '{$arrival_Time}')
  20.         )
  21.  
The `time_diff` column (or whatever you decide to call it) would naturally be an int.
Then, as long as the `time_diff` column is indexed, it becomes VERY fast to merely average this column.
Sep 23 '07 #6
fjm
348 100+
Heya, Frank.

I'd recommend calculating the averages in MySQL simply because then you are fetching fewer rows from the database. The calculations probably take about the same amount of time on either side, but to run the calculations on the PHP side, you have to first fetch every row for that employee.

Now, if you REALLY wanted to speed things up, you could add a `time_diff` column to your table that stores the result of TIMEDIFF(`departure_Time`, `arrival_Time`):

Expand|Select|Wrap|Line Numbers
  1. INSERT
  2. INTO
  3. `t1`
  4. (
  5. .
  6. .
  7. .
  8. `departure_Time`,
  9. `arrival_Time`,
  10. `time_diff`
  11. )
  12. VALUES
  13. (
  14. .
  15. .
  16. .
  17. '{$departure_Time}',
  18. '{$arrival_Time}',
  19. TIMEDIFF('{$departure_Time}', '{$arrival_Time}')
  20. )
  21.  
The `time_diff` column (or whatever you decide to call it) would naturally be an int.
Then, as long as the `time_diff` column is indexed, it becomes VERY fast to merely average this column.
Your good, Pbmods.. That's a great idea! Thank you so much!!

Frank
Sep 23 '07 #7
pbmods
5,821 Expert 4TB
Heya, Frank.

Good luck with your project, and if you ever need anything, post back anytime :)
Sep 23 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: lkrubner | last post by:
This might be an idiot question, but how do you group by timestamps by date? I mean, given a large number of timestamps, spanning many months, how do grab them and say how many are from each day?...
4
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
6
by: Bjørn T Johansen | last post by:
I need to compute the difference of Time fields, in the format HHMM. Is it possible to do the math in the Select? Regards, BTJ --...
5
by: Robert Schuldenfrei | last post by:
Dear NG, I have not heard from anyone about a good book that deals with the concurrency issue in SQL Server using C#. I have PROMISED Nick I would not use record locking and I have used an old...
4
by: Craig G | last post by:
im not too sure how i should be storing the SQL2000 timestamps basically i return a dataset which is used to populate an editable grid. this dataset contains the timestamp. how should i be...
3
by: Eric Rechter | last post by:
Hi, What's the easiest way in php to calculate the hour and minutes (HH:MM) between 2 times? (unix timestamps) thanks in advance! Eric
13
by: In a little while | last post by:
thanks
6
by: moshiur | last post by:
I'm a very new programmer. I have a sorted file and want to calculate delats between the numbers. That is calculate the difference between first number in line 1 and second number in line 2...
7
by: walt | last post by:
Hello, I have been trying to calculate the difference between two date and display the difference in hours and minutes (HH:MM). I can't get it calculate properly and I can't hours and minutes to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.