SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    check difference between dates

    Hi all,

    I have stored two dates in my mysql table. Starting date and ending date for the survey. Now from php, how can i extract if current_date is greater then end_date. The dates are being stored in datetime format in mysql.

    Kindly guide me

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,253
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    you should actually do this with mysql, not php
    Code:
    WHERE end_date < CURRENT_DATE
    CURRENT_DATE is the standard sql function for the current date

    it's more efficient than passing in the current date from php, as in '2012-10-15', because mysql would first have to parse that to make sure it's actually a valid date
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    I agree; it would be better to do it in the DB. If you HAD to do it in PHP though, it's pretty easy.
    PHP Code:
    if ($row['end_date'] < date('Y-m-d H:i:s')) { 
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by kduv View Post
    I agree; it would be better to do it in the DB. If you HAD to do it in PHP though, it's pretty easy.
    PHP Code:
    if ($row['end_date'] < date('Y-m-d H:i:s')) { 
    Even then though, a CASE statement to make a new column will still be a better option if he doesn't want to limit his results to just those.


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
  •