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:
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'.
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