jcstanley Posted March 21, 2007 Share Posted March 21, 2007 Hi I have a query as follows: $query = "SELECT eventid, name, DATE_FORMAT(date, '%d %b %y') AS date, time, type, extra FROM events WHERE type = 'Training' AND date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 21 DAY) ORDER BY 'date' desc"; This displays any events within the next 21 days, but not quite in the right order! The outputted result is: 25 Mar 07 24 Mar 07 08 Apr 07 07 Apr 07 06 Apr 07 As you can see the months are in the correct order but how can the days be reversed? eg: 24 Mar 07 25 Mar 07 06 Apr 07 07 Apr 07 08 Apr 07 Many thanks Link to comment https://forums.phpfreaks.com/topic/43729-solved-problem-ordering-by-date/ Share on other sites More sharing options...
paul2463 Posted March 21, 2007 Share Posted March 21, 2007 you put the orderby date in single ticks try this one $query = "SELECT eventid, name, DATE_FORMAT(date, '%d %b %y') AS `date`, time, type, extra FROM events WHERE type = 'Training' AND date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 21 DAY) ORDER BY `date` desc"; Link to comment https://forums.phpfreaks.com/topic/43729-solved-problem-ordering-by-date/#findComment-212301 Share on other sites More sharing options...
jcstanley Posted March 21, 2007 Author Share Posted March 21, 2007 you put the orderby date in single ticks try this one $query = "SELECT eventid, name, DATE_FORMAT(date, '%d %b %y') AS `date`, time, type, extra FROM events WHERE type = 'Training' AND date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 21 DAY) ORDER BY `date` desc"; Sorry, the above code is what I have already Link to comment https://forums.phpfreaks.com/topic/43729-solved-problem-ordering-by-date/#findComment-212304 Share on other sites More sharing options...
kenrbnsn Posted March 21, 2007 Share Posted March 21, 2007 You're ordering by the ASCII reformated date format, not by the internal format. Change the "as" clause to something other than the name of the date field. Something like: <?php $query = "SELECT eventid, name, DATE_FORMAT(`date`, '%d %b %y') AS formatted_date, `time`, `type`, extra FROM events WHERE type = 'Training' AND date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 21 DAY) ORDER BY `date` desc"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/43729-solved-problem-ordering-by-date/#findComment-212312 Share on other sites More sharing options...
jcstanley Posted March 21, 2007 Author Share Posted March 21, 2007 Thanks Ken That works perfectly! Link to comment https://forums.phpfreaks.com/topic/43729-solved-problem-ordering-by-date/#findComment-212323 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.