SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: How to compare date in MySQL query

  1. #1
    SitePoint Zealot ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    169
    Mentioned
    0 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
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,454
    Mentioned
    39 Post(s)
    Tagged
    1 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
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Location
    San Sebastian, Spain
    Posts
    87
    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 silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,462
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    pullo, using CURRENT_DATE instead of CURRENT_TIMESTAMP will more closely match the original requirements here
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,454
    Mentioned
    39 Post(s)
    Tagged
    1 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.
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  6. #6
    SitePoint Zealot ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    169
    Mentioned
    0 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
  •