Home > Database > Mysql Tutorial > How Can I Format MySQL Dates as DD/MM/YYYY?

How Can I Format MySQL Dates as DD/MM/YYYY?

Barbara Streisand
Release: 2024-12-25 17:01:13
Original
774 people have browsed it

How Can I Format MySQL Dates as DD/MM/YYYY?

Change Date Format to DD/MM/YYYY in MySQL

When working with MySQL, retrieving dates in the format 'YYYY-MM-DD' can be inconvenient. This article provides several methods to transform dates into the desired 'DD/MM/YYYY' format.

PHP Solutions:

  • Using strtotime() and date():

    • Convert the database date to a timestamp using strtotime().
    • Format the timestamp using date() with the desired 'd/m/Y' format.
  • Using DateTime Class:

    • Create a DateTime object from the database date using DateTime::__construct().
    • Use DateTime::format() to format the date to 'd/m/Y'.

MySQL Solution:

  • Using date_format() Function:

    • Use the date_format() function to format the database date to 'd/m/Y' within SQL queries or in the output.

For example, the following MySQL query would display the current date in 'd/m/Y' format:

mysql> select date_format(curdate(), '%d/%m/%Y');
Copy after login

The above is the detailed content of How Can I Format MySQL Dates as DD/MM/YYYY?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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