Draeko Draeko - 1 month ago 13
PHP Question

display DateTime format into readable string in PHP

i have a stored DateTime with this format [2016-10-05 11:58:04]. What i want to do is, display the stored time into this readable format [Wed, 11:58 AM].

Answer

Since you tagged your question with mysqli and php here are solutions for both:

MySQL

You can format the date directly in your query:

select DATE_FORMAT(column_name, "%a, %h:%i %p") AS formatted_date FROM table_name

See docs: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

PHP

If you have this string and need to format it in PHP, use DateTime:

$dt = new DateTime("2016-10-05 11:58:04");
echo $dt->format("D, h:i A"); // Wed, 11:58 AM

Working example: https://3v4l.org/AQeTc

Comments