Alicia Posted October 8, 2012 Share Posted October 8, 2012 Guys, can someone give me a hint on this. Currently i have 2 columns in db to store the date but the structure is in varchar How can I compare the date with these columns because I did try to use str to date function but now working. My script as below. my column name is join and grad and data store in these varchar columns format is like this : 14/03/2001 11:30:00 $now = date("Y-m-d"); $beu_main = mysql_query("SELECT * FROM `students` WHERE STR_TO_DATE( join, '%d/%m/%Y' )<='$now' AND STR_TO_DATE( grad, '%d/%m/%Y' ) > '$now'"); anything wrong with my query or any quick fix? thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/269213-comparing-date/ Share on other sites More sharing options...
Barand Posted October 8, 2012 Share Posted October 8, 2012 Apart from your using varchar for dates (and a useless format) instead of datetime (YYYY-MM-DD HH:ii:ss), and instead of '$now' you could use CURDATE(). I cannot see any obvious errors. Define "not working" and check your data. Quote Link to comment https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383647 Share on other sites More sharing options...
teng84 Posted October 8, 2012 Share Posted October 8, 2012 (edited) maybe type casting would help you Edited October 8, 2012 by teng84 Quote Link to comment https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383655 Share on other sites More sharing options...
Barand Posted October 8, 2012 Share Posted October 8, 2012 shouldn't need typecasting mysql> SELECT STR_TO_DATE('08/10/2012 07:30:00','%d/%m/%Y') = CURDATE() as test; +------+ | test | +------+ | 1 | +------+ 1 row in set, 1 warning (0.00 sec) Quote Link to comment https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383660 Share on other sites More sharing options...
Alicia Posted October 9, 2012 Author Share Posted October 9, 2012 thanks guys but how can i sort this column by showing the latest date first since this is a varchar column? i tried $re= mysql_query("SELECT * FROM `students` ORDER BY STR_TO_DATE( join, '%d/%m/%Y' ) DESC"); but it is not working.. can someone pls assist..thanks Quote Link to comment https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383888 Share on other sites More sharing options...
Barand Posted October 9, 2012 Share Posted October 9, 2012 (edited) Sorry for not spotting this earlier. Your date column name "join" is a reserved word in SQL. You should either rename it (recommended) to something like join_date or enclose the name in backticks in your queries eg STR_TO_DATE(`join`, '%d/%m/%Y') If you check the value of mysql_error() when you get problems you should get there a little faster. Edited October 9, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383895 Share on other sites More sharing options...
Alicia Posted October 9, 2012 Author Share Posted October 9, 2012 i have changed that but it is still not showing the latest date on top.. any idea? thanks Quote Link to comment https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383896 Share on other sites More sharing options...
Barand Posted October 9, 2012 Share Posted October 9, 2012 I set up a test table and this looks OK mysql> SELECT * FROM students; +-----+----------+---------------------+ | pid | username | join | +-----+----------+---------------------+ | 1 | MMDE1 | 01/11/2012 09:30:55 | | 2 | MMDE2 | 05/10/2012 08:55:00 | | 3 | MMDE3 | 08/10/2012 12:25:00 | | 4 | MMDE4 | 06/10/2012 15:30:00 | +-----+----------+---------------------+ 4 rows in set (0.00 sec) mysql> SELECT * FROM students ORDER BY STR_TO_DATE(`join`, '%d/%m/%Y') DESC; +-----+----------+---------------------+ | pid | username | join | +-----+----------+---------------------+ | 1 | MMDE1 | 01/11/2012 09:30:55 | | 3 | MMDE3 | 08/10/2012 12:25:00 | | 4 | MMDE4 | 06/10/2012 15:30:00 | | 2 | MMDE2 | 05/10/2012 08:55:00 | +-----+----------+---------------------+ 4 rows in set, 4 warnings (0.00 sec) Quote Link to comment https://forums.phpfreaks.com/topic/269213-comparing-date/#findComment-1383900 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.