SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    328
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to compare date in MySQL query

    Hi,

    I have
    Code:
     $date = date("Y-m-d");
    How do I get date comparison in MySQL query that looks something like this:
    Code:
    SELECT expired_date
    FROM subscription
    WHERE expired_date > '$date'
    Thanks

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,048
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    An easier/cleaner way to do this is as follows:
    Code MySQL:
    SELECT expired_date FROM subscription WHERE expired_date > CURRENT_TIMESTAMP;

    HTH

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    93
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What Pullo has said above would be better but if you need to convert a string to date in MySQL look at the STR_TO_DATE function.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,340
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    pullo, using CURRENT_DATE instead of CURRENT_TIMESTAMP will more closely match the original requirements here
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,048
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by r937 View Post
    pullo, using CURRENT_DATE instead of CURRENT_TIMESTAMP will more closely match the original requirements here
    Oops. Right you are!
    Thanks rudy.

  6. #6
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    328
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I knew I can rely on your guys here.


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
  •