I have a table that contains a datetime field type, and I want to sort it on descending
My Datetime field have like this format:
12:17:23 PM / Thu, May 26th, 2016
And my code is
if($stmt=$connc->query("SELECT * FROM static_vehicle_cat ORDER BY UNIX_TIMESTAMP(edit_date_time) DESC"))
if column type is varchar can used this code:
SELECT
DATE_FORMAT(
STR_TO_DATE(
`edit_date_time`,
'%h:%i:%s %p / %a,%b %D, %Y'
),
'%Y-%m-%d %H:%i:%s'
)
FROM
`static_vehicle_cat`
ORDER BY DATE_FORMAT(
STR_TO_DATE(
`edit_date_time`,
'%h:%i:%s %p / %a,%b %D, %Y'
),
'%Y-%m-%d %H:%i:%s'
) DESC
if column type is datetime.alter table add new column type varchar and copy edit_date_time column to new column. after use code
We can easily sort date time field using query Select * from storages order by last_updated desc;
Here last_updated column having date time field type