Cyto Posted March 14, 2012 Share Posted March 14, 2012 Hello, I would like to order by date that contains day and month, like this: 14-3 Is this possible? $sql = mysql_query("SELECT * FROM date WHERE online='1'"); Quote Link to comment https://forums.phpfreaks.com/topic/258946-mysql-order-by-date/ Share on other sites More sharing options...
requinix Posted March 14, 2012 Share Posted March 14, 2012 Why aren't you using a DATE or DATETIME column? Quote Link to comment https://forums.phpfreaks.com/topic/258946-mysql-order-by-date/#findComment-1327471 Share on other sites More sharing options...
Cyto Posted March 14, 2012 Author Share Posted March 14, 2012 because i'm adding it from admin. See it as adding a event in a calendar. Quote Link to comment https://forums.phpfreaks.com/topic/258946-mysql-order-by-date/#findComment-1327475 Share on other sites More sharing options...
Psycho Posted March 14, 2012 Share Posted March 14, 2012 because i'm adding it from admin. See it as adding a event in a calendar. Huh? I'm not following. You should always store dates/times as appropriate date and/or time formats in the database. You can always convert the value into whatever format you want in the output. Quote Link to comment https://forums.phpfreaks.com/topic/258946-mysql-order-by-date/#findComment-1327478 Share on other sites More sharing options...
Cyto Posted March 14, 2012 Author Share Posted March 14, 2012 Is there a way to covert 14-3 to what you mean and order it? Quote Link to comment https://forums.phpfreaks.com/topic/258946-mysql-order-by-date/#findComment-1327480 Share on other sites More sharing options...
cpd Posted March 14, 2012 Share Posted March 14, 2012 Is there a way to covert 14-3 to what you mean and order it? Change the field type to DATE and alter your code to format the date as YYYY-MM-DD. Then either write a script to convert existing dates, even though you don't have the year :s, or do it manually (long). Quote Link to comment https://forums.phpfreaks.com/topic/258946-mysql-order-by-date/#findComment-1327487 Share on other sites More sharing options...
Cyto Posted March 15, 2012 Author Share Posted March 15, 2012 I founded out myself. Using STR_TO_DATE(date, '%d-%m') ASC. Quote Link to comment https://forums.phpfreaks.com/topic/258946-mysql-order-by-date/#findComment-1327499 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.