Avatar of lhutton
lhutton
 asked on

Sorting PHP Dates With DATE_FORMAT

I have a tinytext field that takes values in the format date("d M, Y H:i") and want to sort it in descending chronological order.

I have tried the following SQL, using the Reference Manual as a guide, but it will not sort in descending order:

SELECT field FROM table ORDER BY DATE_FORMAT(field, '%d %b, %Y %H:%i') DESC



If I should alter the format in which the field takes values using the date function, please supply a method of altering the existing values, along with the amended date function and SQL query.
PHPMySQL Server

Avatar of undefined
Last Comment
lhutton

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ee_rlee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Guy Hengel [angelIII / a3]

>I have a tinytext field that takes values in the format date("d M, Y H:i")
to start with: why a tinytext field, and not a datetime data type? would make your life much easier.

now: DATE_FORMAT formats a date into a string.
you HAVE a string, and want it sorted by date value, hence you want to use the function str_to_date:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date


lhutton

ASKER
Thank you!
lhutton

ASKER
angelIII: tinytext, because that was what I was given to begin with, ages ago; not my choice.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck