Draeko
Draeko

Reputation: 111

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].

Upvotes: 0

Views: 198

Answers (4)

jszobody
jszobody

Reputation: 28911

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

Upvotes: 5

Claude
Claude

Reputation: 11

You could do something like that :

$date = "2016-10-05 11:58:04";
$newDateTime = date('D, h:i A', strtotime($date));

Upvotes: 0

Ace_Gentile
Ace_Gentile

Reputation: 244

Should be

$dat = '2016-10-05 11:58:04';
$your_date = date("D, h:i A", strtotime($dat));

Where

D = day of the week
h = hour based on 2 x 12h day
i = minutes
A = AM or PM

function reference: http://php.net/manual/en/function.date.php

Upvotes: 1

santosh
santosh

Reputation: 118

$date = '2016-10-05 11:58:04'

$human_readable_date = date('d-M-Y H:i:s', strtodate($date))

--output : 05-Oct-2016 11:58:04

Upvotes: 0

Related Questions