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.