Jump to content

Strange Sort by Date and time problem


alexsmith2709

Recommended Posts

Hi,

I've done a search and cant find anything related to my problem.

I have a newsfeed on my site and i want it to display with the latest news at the top.

I have this code:

$get_news = "SELECT title, text, DATE_FORMAT(datetime, '%e %b %Y at %T') AS datetime FROM newsfeed ORDER BY datetime DESC";

 

This works fine on one of my sites but on another i've just hit a problem. It seems to sort only by the day, not the rest of the date.

I made a news post today with date and time of "27 Mar 2011 at 12:54:02" but a post from "28 Jan 2011 at 19:59:52" is above it.

 

Any ideas why this is happening?

 

Thanks,

Alex

Link to comment
https://forums.phpfreaks.com/topic/231844-strange-sort-by-date-and-time-problem/
Share on other sites

User a different alias name in - AS datetime

 

When you use an alias name that is the same as a column name, the alias data value is used where you reference that name in the query instead of the actual column. Your query is ORDERing BY the formatted datetime instead of the actual datetime. DATE and DATETIME data types can be ordered correctly. Your formated datetime cannot.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.