Home >Database >Mysql Tutorial >How to Format Dates as dd/mm/yyyy in PHP and MySQL?

How to Format Dates as dd/mm/yyyy in PHP and MySQL?

Barbara Streisand
Barbara StreisandOriginal
2024-12-20 09:35:101036browse

How to Format Dates as dd/mm/yyyy in PHP and MySQL?

How to Display Dates in dd/mm/yyyy Format in PHP MySQL

Problem:

By default, MySQL stores dates in the 'YYYY-MM-DD' format. However, it is often desirable to display dates in the more familiar 'dd/mm/yyyy' format. How can this be achieved using PHP MySQL?

Answers:

Using PHP:

  1. Convert the date from the database to a timestamp using strtotime().
  2. Format the timestamp in the desired format using date().

Example:

Note that this method is limited to dates within the range of 1970 to 2038 due to timestamp limitations.

Using MySQL:

The date_format() function in MySQL can be used to format dates.

Example:

This will return the current date in the 'dd/mm/yyyy' format.

Using DateTime Class:

The DateTime class in PHP provides a more flexible approach that avoids the limitations of timestamps.

Example:

This will output the date as '19/03/2010'.

The above is the detailed content of How to Format Dates as dd/mm/yyyy in PHP and MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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