"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to format dates as dd/mm/yyyy in PHP and MySQL?

How to format dates as dd/mm/yyyy in PHP and MySQL?

Posted on 2025-03-13
Browse:206

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:

$timestamp = strtotime($date_from_db);
echo date('d/m/Y', $timestamp);

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:

SELECT date_format(curdate(), '%d/%m/%Y');

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:

$date = new DateTime('2010-03-19');
echo $date->format('d/m/Y');

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

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3