Reputation:
I need your help with datetime coversion. Inside my database I have date of comments entered like this:
Datetime: 2012-05-08 14:44:53
How can I make it display something close to this
May 15, 2012 2:44PM
Thanks for your time and patience.
Upvotes: 3
Views: 148
Reputation: 263723
DATE_FORMAT() is the answer to your question. It has several formats of date on this link
SELECT DATE_FORMAT(NOW(), '%M %d, %Y %h:%i %p') as FormattedDate;
View The Output Here [SQLFiddle]
%M Month name (January..December)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
%h Hour (01..12)
%i Minutes, numeric (00..59)
%p AM or PM
Upvotes: 2
Reputation: 21272
You should use MySQL DATE_FORMAT()
function.
Reference:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Example query:
SELECT DATE_FORMAT(`date`, '%a %d, %Y %l:%s%p') AS `myDate`;
Upvotes: -1
Reputation: 1224
You need to use strtotime()
to convert to a Unix timestamp. You can then use date()
to display the exact format you need. Something like this:
$unix = strtotime($datetime);
echo date(F j Y g:iA, $unix);
Upvotes: 0