Home >Database >Mysql Tutorial >How to Change MySQL and PHP Date Formats to dd/mm/yyyy?

How to Change MySQL and PHP Date Formats to dd/mm/yyyy?

Susan Sarandon
Susan SarandonOriginal
2024-12-21 17:32:11348browse

How to Change MySQL and PHP Date Formats to dd/mm/yyyy?

How to Alter Date Format to dd/mm/yyyy in PHP MySQL

When retrieving dates from a MySQL database, you may encounter the default 'YYYY-MM-DD' format. If you prefer the dd/mm/yyyy format, there are several approaches to achieve this transformation.

PHP Solutions:

  • Timestamp Conversion:
    Convert the date string to a timestamp using strtotime(). Then, format it to the desired format using date().
$timestamp = strtotime($date_from_db);
echo date('d/m/Y', $timestamp);
  • DateTime Class:
    Use the DateTime class to parse the date and format it. This method is advantageous as it doesn't have the 1970-2038 limitation of timestamps.
$date = new DateTime('2010-03-19');
echo $date->format('d/m/Y');

MySQL Solution:

  • date_format() Function:
    Apply the date_format() function to modify the date format within the MySQL query.
SELECT date_format(curdate(), '%d/%m/%Y');

The above is the detailed content of How to Change MySQL and PHP Date Formats to dd/mm/yyyy?. 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