SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot stikkybubble's Avatar
    Join Date
    Sep 2006
    Location
    Pluto
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL: can I combine str_to_date and datediff?

    I have a database where dates were incorrectly formatted (so they are stored as varchar). Now I want to be able to only display dates from the past (including today).
    I have found the mysql functions str_to_date and datediff, and have been trying to construct a mysql query which will combine them (using now() as the second date for datediff).
    So far this has been a miserable failure. I'm quite new to php and mysql, and I'm not at all sure that what I am trying to do is even possible.
    The table is called 'dates' and the column with the (wrongly formatted, e.g. 31/06/2009) date is called 'date'.
    Here is the relevant piece of code - although I have been through several versions:

    $query = "SELECT date FROM dates WHERE ((DateDiff(STR_TO_DATE('date', '&#37;d/%M/%Y'),NOW())) <= 0)";
    $result = mysql_query($query, $dbh);
    while($row = mysql_fetch_row($result))
    {
    echo "<option>";
    echo $row[0];
    echo "</option>";
    }
    mysql_close($dbh);

    PS wasn't sure if this should be in PHP forum instead.
    Apologies: did not see the mySQL forum there until after posting, sorry. Is it possible for a moderator to move this?
    Last edited by stikkybubble; Jan 1, 2009 at 16:59. Reason: wrong forum

  2. #2
    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)
    the first thing you should fix is the STR_TO_DATE function call

    you're asking it to convert the value of 'date' (which is a string)

    what you want is to convert the value of the `date` column, instead

    what STR_TO_DATE does is produce a date, and you don't need to use DATEDIFF, you can just use a comparison operator...
    Code:
    WHERE STR_TO_DATE(`date`,'&#37;d/%M/%Y') <= CURRENT_DATE
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Zealot stikkybubble's Avatar
    Join Date
    Sep 2006
    Location
    Pluto
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much: I don't have time to test this just now, but that certainly looks rational to me!

    Thanks again!

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good stuff, taken note of this.

    However I used to do this in a slightly different method.

    As I store inputed time in seconds

    My query would be like

    Code MySQL:
    WHERE time_column >= ' " time() " '


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
  •