The MySQL DATE_FORMAT() function formats a date value with a given specified format. You may also use MySQL DATE_FORMAT() on datetime values and use some of the formats specified for the TIME_FORMAT() function to format the time value as well.
Use STR_TO_DATE() method from MySQL to convert. The syntax is as follows wherein we are using format specifiers. The format specifiers begin with %. SELECT STR_TO_DATE(yourDateColumnName,'%d.
Guessing you probably just want to format the output date? then this is what you are after
SELECT *, DATE_FORMAT(date,'%d/%m/%Y') AS niceDate
FROM table
ORDER BY date DESC
LIMIT 0,14
Or do you actually want to sort by Day before Month before Year?
You can use STR_TO_DATE()
to convert your strings to MySQL date values and ORDER BY
the result:
ORDER BY STR_TO_DATE(datestring, '%d/%m/%Y')
However, you would be wise to convert the column to the DATE
data type instead of using strings.
SELECT DATE_FORMAT(somedate, "%d/%m/%Y") AS formatted_date
..........
ORDER BY formatted_date DESC
Use:
SELECT DATE_FORMAT(NAME_COLUMN, "%d/%l/%Y") AS 'NAME'
SELECT DATE_FORMAT(NAME_COLUMN, "%d/%l/%Y %H:%i:%s") AS 'NAME'
Reference: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
SELECT DATE_FORMAT(COLUMN_NAME, "%d/%m/%Y %h:%i %p");
OR
SELECT DATE_FORMAT("2019-05-10 19:30:10", "%d/%m/%Y %h:%i %p");
OUTPUT is 10/05/2019 07:30 PM
for my case this worked
str_to_date(date, '%e/%m/%Y' )
If the hour is important, I used str_to_date(date, '%d/%m/%Y %T' )
, the %T
shows the hour in the format hh:mm:ss
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With