Home > Database > Mysql Tutorial > How to Display DATETIME as 'DD-MM-YYYY HH:MM:SS' in MySQL?

How to Display DATETIME as 'DD-MM-YYYY HH:MM:SS' in MySQL?

Patricia Arquette
Release: 2024-12-04 06:38:11
Original
659 people have browsed it

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
);
Copy after login

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;
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template