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
Release: 2024-12-20 09:35:10
Original
995 people have browsed it

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!

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