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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.