SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Difference in minutes between two dates

    hi, hope in your help.

    this query working:
    Code:
    mysql> SELECT
    	DATEDIFF(
    		STR_TO_DATE(
    			'15/01/2012 08:02:00',
    			'%d/%m/%Y %h:%i:%s'
    		),
    		STR_TO_DATE(
    			'14/01/2012 08:00:00',
    			'%d/%m/%Y %h:%i:%s'
    		)
    	) AS output;
    +--------+
    | output |
    +--------+
    |      1 |
    +--------+
    I need difference in minutes between two dates and tried this version:
    Code:
    mysql> 
    SELECT
    	DATE_ADD(
    		STR_TO_DATE(
    			'15/01/2012 08:02:00',
    			'%d/%m/%Y %h:%i:%s'
    		) INTERVAL STR_TO_DATE(
    			'14/01/2012  08:00:00',
    			'%d/%m/%Y %h:%i:%s') MINUTE
    		) AS output;
    1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERVAL STR_TO_DATE(
    			'14/01/2012  08:00:00',
    			'%d/%m/%Y %h:%i:%s' MINUTE' at line 6
    Can you help me?
    thank you
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  2. #2
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Solved ?:
    Code:
    mysql> SELECT
    	TIME_TO_SEC(
    		TIMEDIFF(
    			STR_TO_DATE(
    				'15/01/2012 08:02:00',
    				'%d/%m/%Y %h:%i:%s'
    			),
    			STR_TO_DATE(
    				'14/01/2012 08:00:00',
    				'%d/%m/%Y %h:%i:%s'
    			)
    		)
    	) / 60 AS output;
    +--------+
    | output |
    +--------+
    | 1442   |
    +--------+
    1 row in set
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,322
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    you cannot add an interval which is a date

    and once again, you are making things more difficult for yourself needlessly

    difference in minutes --
    Code:
    SELECT ( UNIX_TIMESTAMP('2012-01-15 08:02:00') -
             UNIX_TIMESTAMP('2012-01-14 08:00:00') ) / 60 AS diff_minutes
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    you cannot add an interval which is a date

    and once again, you are making things more difficult for yourself needlessly

    difference in minutes --
    Code:
    SELECT ( UNIX_TIMESTAMP('2012-01-15 08:02:00') -
             UNIX_TIMESTAMP('2012-01-14 08:00:00') ) / 60 AS diff_minutes
    thanks.
    Code:
    mysql> SELECT
    	ROUND(
    		(
    			UNIX_TIMESTAMP('2012-01-15 08:02:00') - UNIX_TIMESTAMP('2012-01-14 08:00:00')
    		) / 60,
    		0
    	) AS diff_minutes;
    +--------------+
    | diff_minutes |
    +--------------+
    | 1442         |
    +--------------+
    1 row in set
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •