ShaolinF Posted November 26, 2009 Share Posted November 26, 2009 Hi Guys I am trying to get all records that are less than the current date. See code below followed by problem: CREATE TABLE courses ( course_id integer auto_increment NOT NULL, course_finish_date varchar(10), PRIMARY KEY(course_id) ); SELECT * FROM courses WHERE course_finish_date < CURDATE(); The SELECT statement works but it doesnt return any results (when it should). I suspect the reason for this is because course_finish_date is a varchar and not a date/timestamp. The column stores the date in the following format: DD/MM/YYYY - Is there any way I can convert it to a date and THEN compare ? Link to comment https://forums.phpfreaks.com/topic/183036-comparing-dates/ Share on other sites More sharing options...
fenway Posted November 26, 2009 Share Posted November 26, 2009 Yup... add a new DATE column, convert using STR_TO_DATE(), then drop the old one, and then mysql date math is a joke. Link to comment https://forums.phpfreaks.com/topic/183036-comparing-dates/#findComment-966000 Share on other sites More sharing options...
ShaolinF Posted November 26, 2009 Author Share Posted November 26, 2009 Thanks. I couldnt use the STR_TO_DATE() but I managed to use strtotime() to convert the date in a 10 digit date number. Now, I need to compare that date number will the current date to workout the difference between the two. I tried DATEDIFF(course_finish_date, CURDATE()) < 0 - Namely all dates that come BEFORE today, but it returns no results when it should. Any ideas where Im going wrong ? Link to comment https://forums.phpfreaks.com/topic/183036-comparing-dates/#findComment-966136 Share on other sites More sharing options...
Mchl Posted November 26, 2009 Share Posted November 26, 2009 Now you can use FROM_UNIXTIME to convert it to DATE (or DATETIME) format. Then just compare it with CURDATE() Link to comment https://forums.phpfreaks.com/topic/183036-comparing-dates/#findComment-966148 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.