Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

mysql - calculating difference between two times in query

Posted on 2012-04-08
3
Medium Priority
?
437 Views
Last Modified: 2012-04-09
0 down vote favorite
share [g+] share [fb] share [tw]
      

I know I could use PHP to do this, but wanted to find out if there was a way to calculate the difference between two times using just a query? I tried the query below, but it's returning NULL for the time difference.

The data in my table is stored as:

| created    | changed    |
+------------+------------+
| 1333643004 | 1333643133 |

Open in new window


I wanted to figure out a way to return:

| 2012-04-05 09:23:24 | 2012-04-05 09:25:33 |     00:02:09 |

Open in new window

I tried:

SELECT 
    FROM_UNIXTIME(created) AS created, 
    FROM_UNIXTIME(changed) AS changed, 
    TIMEDIFF ( changed, created ) / 60 AS timediff 
FROM content
    WHERE id = 45;

Open in new window


Which yielded:

| 2012-04-05 09:23:24 | 2012-04-05 09:25:33 | NULL |

Open in new window

0
Comment
Question by:n00b0101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 37822266
When I tried that, FROM_UNIXTIME is ignored in the TIMEDIFF statement.  TIMEDIFF uses the data from the columns and ignores the 'AS' variables.  Plus I think you had the order wrong in that statement.  And you shouldn't use function names as variable names.

SELECT
    created, changed, (TIMEDIFF ( created, changed ) / 60) AS thediff
FROM content
    WHERE id = 45;
0
 
LVL 24

Expert Comment

by:johanntagle
ID: 37822275
timediff requires its inputs to be both datetime or both date datatype.  So it should be:

select timediff(from_unixtime(changed), from_unixtime(created)) from content where id=45;
0
 
LVL 24

Expert Comment

by:johanntagle
ID: 37822294
Note also that timediff returns the value in TIME format (hh:mm:ss) so no need to divide by 60.  If you need your result to be something like 1.23 minutes use time_to_sec() (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time-to-sec) on the output of timediff then divide by 60.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question