Home >Database >Mysql Tutorial >How to Display DATETIME as 'DD-MM-YYYY HH:MM:SS' in MySQL?
How to Set Default DATETIME Format to 'DD-MM-YYYY HH:MM:SS' in MySQL CREATE TABLE Statement
While MySQL stores DATETIME values in the 'YYYY-MM-DD HH:MM:SS' format, you may encounter situations where you need to specify a different default format, such as 'DD-MM-YYYY HH:MM:SS'. This article guides you through setting up this specific format during table creation using the MySQL command line.
Solution:
Although it's not possible to explicitly set the default format to 'DD-MM-YYYY HH:MM:SS' using the CREATE TABLE statement, you can utilize MySQL's time format functions to achieve the desired display format. One such function is DATE_FORMAT:
CREATE TABLE table_name ( datetime_field DATETIME );
After creating the table, you can use the DATE_FORMAT function in queries to display the datetime_field column in the desired format:
SELECT DATE_FORMAT(datetime_field, '%d-%m-%Y %H:%i:%s') FROM table_name;
The above is the detailed content of How to Display DATETIME as 'DD-MM-YYYY HH:MM:SS' in MySQL?. For more information, please follow other related articles on the PHP Chinese website!