Use MySQL's DATE_FORMAT function to customize the display format of date and time
Date and time are very important data types in the database. MySQL provides the DATE_FORMAT function to customize the display format of date and time. By using this function, we can flexibly control the output format of date and time to meet different needs.
The syntax for using the DATE_FORMAT function is as follows:
DATE_FORMAT(date, format)
Among them, the date parameter is the date or time value to be formatted, and the format parameter is a string specifying the output format.
Let’s demonstrate some commonly used custom date and time formats:
Year-Month-Day
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d') as formatted_date;
Output result example: 2023- 07-15
Year/Month/Day
SELECT DATE_FORMAT(NOW(), '%Y/%m/%d') as formatted_date;
Output result example: 2023/07/15
Year- Month-day hour: minute: second
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s') as formatted_datetime;
Output result example: 2023-07-15 12:34:56
SELECT DATE_FORMAT(NOW(), '%b') as month_abbreviation;
SELECT DATE_FORMAT(NOW(), '%M') as month_full_name;
SELECT DATE_FORMAT(NOW(), '%W') as weekday;
SELECT DATE_FORMAT(TIMEDIFF('2023-07-15 12:00:00', NOW()), '%H:%i:%s') as time_diff;
The above is the detailed content of Use MySQL's DATE_FORMAT function to customize the display format of date and time. For more information, please follow other related articles on the PHP Chinese website!