Results 1 to 4 of 4
Jan 1, 2009, 15:54 #1
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', '%d/%M/%Y'),NOW())) <= 0)";
$result = mysql_query($query, $dbh);
while($row = mysql_fetch_row($result))
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 15:59. Reason: wrong forum
Jan 1, 2009, 16:01 #2
- Join Date
- Jul 2002
- Toronto, Canada
- 49 Post(s)
- 1 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...
WHERE STR_TO_DATE(`date`,'%d/%M/%Y') <= CURRENT_DATE
Jan 2, 2009, 12:15 #3
Thank you so much: I don't have time to test this just now, but that certainly looks rational to me!
Jan 5, 2009, 03:10 #4
- Join Date
- Sep 2008
- 0 Post(s)
- 0 Thread(s)