473,394 Members | 1,843 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,394 software developers and data experts.

Elapsed time between 2 DateTime values

MySQL Version 4.0.20 on a Linux server.

How does one get the elapsed time between (2) DateTime values?
I need the answer to the nearest minute.

Is upgrading to Ver 5 with its more robust date/time functions the only
solution?

You can directly subtract 2 DateTime values and a long integer results.
What is that number?

Thanks in advance
Thomas Bartkus

Jul 20 '05 #1
6 24780
Thomas Bartkus wrote:
MySQL Version 4.0.20 on a Linux server.

How does one get the elapsed time between (2) DateTime values?
I need the answer to the nearest minute.

Is upgrading to Ver 5 with its more robust date/time functions the only
solution?

You can directly subtract 2 DateTime values and a long integer results.
What is that number?


mysql> select *,date2-date1 from datetest;
+---------------------+---------------------+-------------+
| date1 | date2 | date2-date1 |
+---------------------+---------------------+-------------+
| 2004-06-29 20:26:09 | 2004-06-29 20:28:47 | 238 |
+---------------------+---------------------+-------------+
1 row in set (0.00 sec)
date2-date1 is difference in seconds. If you want to get result in
minutes, you can divide it by 60 (One minute contains 60 seconds).

mysql> select *,((date2-date1)/60) from datetest;
+---------------------+---------------------+--------------------+
| date1 | date2 | ((date2-date1)/60) |
+---------------------+---------------------+--------------------+
| 2004-06-29 20:26:09 | 2004-06-29 20:28:47 | 3.9666666666667 |
+---------------------+---------------------+--------------------+
1 row in set (0.00 sec)

And you can of course round the result

mysql> select *,round((date2-date1)/60) from datetest;
+---------------------+---------------------+-------------------------+
| date1 | date2 | round((date2-date1)/60) |
+---------------------+---------------------+-------------------------+
| 2004-06-29 20:26:09 | 2004-06-29 20:28:47 | 4 |
+---------------------+---------------------+-------------------------+
1 row in set (0.02 sec)

Please read about round() function from the manual:
http://dev.mysql.com/doc/mysql/en/Ma...functions.html

Because you might want to use TRUNCATE() or FLOOR() instead.
Jul 20 '05 #2
In article <pa***************************@comcast.net>, Thomas Bartkus
<th***********@comcast.net> wrote:
MySQL Version 4.0.20 on a Linux server.

How does one get the elapsed time between (2) DateTime values?
I need the answer to the nearest minute.


One way is to convert both to unix timestamps, subtract, and then
convert the result back to a datetime:

mysql> select * from foo;
+---------------------+---------------------+
| bar | baz |
+---------------------+---------------------+
| 2004-06-26 00:00:00 | 2004-06-26 01:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> select sec_to_time(unix_timestamp(baz) - unix_timestamp(bar))
as elapsed from foo;
+----------+
| elapsed |
+----------+
| 01:00:00 |
+----------+
1 row in set (0.00 sec)

--
Jul 20 '05 #3
"John Doherty" <jd******@nowhere.null.not> wrote in message
news:jd***********************@192.168.2.178...
In article <pa***************************@comcast.net>, Thomas Bartkus
<th***********@comcast.net> wrote:
MySQL Version 4.0.20 on a Linux server.

How does one get the elapsed time between (2) DateTime values?
I need the answer to the nearest minute.


One way is to convert both to unix timestamps, subtract, and then
convert the result back to a datetime:

mysql> select * from foo;
+---------------------+---------------------+
| bar | baz |
+---------------------+---------------------+
| 2004-06-26 00:00:00 | 2004-06-26 01:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> select sec_to_time(unix_timestamp(baz) - unix_timestamp(bar))
as elapsed from foo;
+----------+
| elapsed |
+----------+
| 01:00:00 |
+----------+
1 row in set (0.00 sec)


Bingo! That does it.

Thank you so much
Thomas Bartkus
Jul 20 '05 #4
"Aggro" <sp**********@yahoo.com> wrote in message
news:Id***************@read3.inet.fi...

mysql> select *,date2-date1 from datetest;
+---------------------+---------------------+-------------+
| date1 | date2 | date2-date1 |
+---------------------+---------------------+-------------+
| 2004-06-29 20:26:09 | 2004-06-29 20:28:47 | 238 |
+---------------------+---------------------+-------------+
1 row in set (0.00 sec)
date2-date1 is difference in seconds.


In the example you show, the differences between date2 and date1 is 158
seconds. The 238 result might be meaningful, but it sure ain't seconds!

What in heck is MySQL returning when you subtract (2) datetime values?
Thomas Bartkus


Jul 20 '05 #5
Thomas Bartkus wrote:
In the example you show, the differences between date2 and date1 is 158
seconds. The 238 result might be meaningful, but it sure ain't seconds!


You are correct. I should have checked the results before posting this.
I am very sorry about this.

mysql> select date2+0,date1+0,date2-date1 from datetest;
+----------------+----------------+-------------+
| date2+0 | date1+0 | date2-date1 |
+----------------+----------------+-------------+
| 20040629202847 | 20040629202609 | 238 |
+----------------+----------------+-------------+
1 row in set (0.00 sec)

This should give more information about what the result would be. It is
not actually meaningfull at all. The values of dates in number format
are just formed as "yyyymmddhhmmss". So it contains the same information
as the string, but instead of using characters, each digit is actually
an integer. That format might be usefull for someone, but you can't use
that format to calculate differences.

So like you have already been adviced on the other post, use the
unix_timestamp() function, to convert time into seconds.
Jul 20 '05 #6
----- Original Message -----
From: "Aggro" <sp**********@yahoo.com>
Newsgroups: mailing.database.mysql
Sent: Thursday, July 01, 2004 5:17 PM
Subject: Re: Elapsed time between 2 DateTime values

Thomas Bartkus wrote:
In the example you show, the differences between date2 and date1 is 158
seconds. The 238 result might be meaningful, but it sure ain't seconds!


You are correct. I should have checked the results before posting this.
I am very sorry about this.


Please - please. Don't apologize.
Your help is most appreciated!

I didn't mean to tweak you for making an error. It was only me pumping for
*more* of your valuable info.
And - you picked it apart to shed light on how MySQL handles date
arithmetic.
And - you posted it for all to see!

A thousand thanks.
Thomas Bartkus

Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: NotGiven | last post by:
Below is a good elapsed time function I found. However, I'd like to return total seconds instead of broken down into days, hours, minutes & seconds. In other words, I want "125" instead of "2...
5
by: JSmith | last post by:
I want to determine the elapsed time between two events. What is the best way to do this?
2
by: ad | last post by:
I have a click event of a button, it need more time to accomplished the job. How can I estimate elapsed time it cost?
3
by: Bill Nguyen | last post by:
I need to display elapsed time for a process in VB.NET in secods. What's the routine to do this? Thanks Bill
4
by: Jim in Arizona | last post by:
I want to make buttons available or not available based on how much time has elapsed. An example: If NOW() 8 hours then btnOne.Visible = False Thanks. Jim
12
by: Spitfire | last post by:
I've a requirement to find the elapsed time between two function calls. I need to find the time elapsed accurate to 1 millisecond. The problem I'm facing right now is that, I'm using the 'time()'...
0
by: Gabriel Genellina | last post by:
En Fri, 02 May 2008 16:13:41 -0300, Simon Pickles <sipickles@googlemail.comescribió: Two options: a) You can construct a datetime object with that info, using...
9
by: Ross | last post by:
I'm a newbie at this, and have searched a lot but can't find something that seems appropriate for measuring a recurring elapsed time. Creating an object with: var mydate = new Date(); seems...
1
by: remya1000 | last post by:
I’m using VB.net 2003 application program. I am trying to do a select statement whereby I'm searching between 2 datetime values that are being stored as datetime. records are stored inside...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.