SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find time difference between two dates

    Hi Guys,

    I have the following MySQL query and I would like to find the time difference in days, mins, hours between the current time and the "end_date" field (which is a datetime filed).

    PHP Code:
    select auctions.end_date,domains.* from domains left join auctions on auctions.domain_id domains.id where end_date now() and active='1' order by end_date desc limit 10 
    Any ideas how this can be achieved in the above query?

    Thanks

  2. #2
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try for days:
    Code:
    round((end_date > now())/86400)
    the same can be done for other part.

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kornev View Post
    Try for days:
    Code:
    round((end_date > now())/86400)
    the same can be done for other part.
    Hmmm, I do not think this works. The query that I originally posted returns results, but when I insert round() it returns an empty set.

    Any ideas?

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    end_date > now() returns either 0 or 1, because it's either true or false

    divide 0 or 1 by 86400 using integer arithmetic, and the answer is 0

    zaggs, are you using an application language like php? please show how you would accomplish the formatting in that language
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    end_date > now() returns either 0 or 1, because it's either true or false

    divide 0 or 1 by 86400 using integer arithmetic, and the answer is 0

    zaggs, are you using an application language like php? please show how you would accomplish the formatting in that language
    Hi, yes I am using php. You mean use something like date_format() ?

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    something like? it would have to be a bit more than just a function

    you want a time difference, right? well you can do that in mysql with this --
    Code:
    SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(end_date) as diff_seconds
      FROM ...
    but then the tricky part is translating this into words
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    something like? it would have to be a bit more than just a function

    you want a time difference, right? well you can do that in mysql with this --
    Code:
    SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(end_date) as diff_seconds
      FROM ...
    but then the tricky part is translating this into words
    Great stuff! That's all I needed

    Thank you.


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
  •