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
Release: 2025-01-04 10:02:39
Original
608 people have browsed it

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);
Copy after login
  • 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');
Copy after login

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');
Copy after login

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!

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