Difference between two dates in MySQL

Difference between two dates in MySQL

Asked on December 14, 2018 in SQL Server.
Add Comment


  • 5 Answer(s)

    To get the solution, we are going to use TIMESTAMPDIFF 

    SELECT TIMEDIFF('2007-12-31 10:02:00','2007-12-30 12:01:01');
    -- result: 22:00:59, the difference in HH:MM:SS format
     
    SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00');
    -- result: 79259 the difference in seconds
    
    Answered on December 14, 2018.
    Add Comment

    In order to work with DATE columns , you have to try DATEDIFF() and then multiply by 24 hours, 60 min, 60 secs . Refer this mysql link

    http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

    For example

    mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30
    
    Answered on December 14, 2018.
    Add Comment

    Use this code,

    SELECT TIMESTAMPDIFF(HOUR,NOW(),'2013-05-15 10:23:23')
       calculates difference in hour.(for days--> you have to define day replacing hour
    SELECT DATEDIFF('2012-2-2','2012-2-1')
     
    SELECT TO_DAYS ('2012-2-2')-TO_DAYS('2012-2-1')
    
    Answered on December 14, 2018.
    Add Comment
    SELECT TIMESTAMPDIFF(HOUR,NOW(),'2013-05-15 10:23:23')
       calculates difference in hour.(for days--> you have to define day replacing hour
    SELECT DATEDIFF('2012-2-2','2012-2-1')
    
    SELECT TO_DAYS ('2012-2-2')-TO_DAYS('2012-2-1')
    Answered on January 14, 2019.
    Add Comment

    If you have two dates, and you want to know how many days between them, you can use the DATEDIFF(date1, date2) function as shown below:

    SELECT DATEDIFF(DATE('1997-02-28'), DATE('1997-03-01'))
       FROM DUAL;
       -1
    Answered on January 14, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.