简体   繁体   中英

Datetime Display In MySQL

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.

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

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);

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`;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM