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

How to Change MySQL Date Format to dd/mm/yyyy in PHP and SQL?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-04 10:02:39613browse

How to Change MySQL Date Format to dd/mm/yyyy in PHP and SQL?

Change Date Format to dd/mm/yyyy in PHP MySQL

In MySQL, date is stored in 'YYYY-MM-DD' format by default. This format may not be user-friendly for certain applications. This article presents several methods to transform the date format to dd/mm/yyyy.

PHP-based Solutions:

  • Using strtotime() and date(): Convert the date to a timestamp using strtotime(), then format it using date(). For example:
$timestamp = strtotime($date_from_db);
echo date('d/m/Y', $timestamp);
  • Using DateTime Class: This solution is not limited by the 1970-2038 timestamp range. Use the DateTime::__construct() method to parse the date, then format it using DateTime::format(). For example:
$date = new DateTime('2010-03-19');
echo $date->format('d/m/Y');

MySQL-based Solutions:

  • Using date_format() Function: This function allows you to format dates in SQL queries. For example:
SELECT date_format(curdate(), '%d/%m/%Y');

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