Detailed explanation of formatted date in mysql

 

 The DATE_FORMAT() function is used to display date/time data in different formats.

 
1
DATE_FORMAT( date ,format)

The format of the format parameter is

%a Abbreviated week name
%b Abbreviated month name
%c month, value
%D day of the month with an English prefix
%d Day of the month, numeric (00-31)
%e day of the month, numeric (0-31)
%f microseconds
%H Hours (00-23)
%h Hours (01-12)
%I Hours (01-12)
%i minutes, numeric (00-59)
%j Day of the Year (001-366)
%k Hours (0-23)
%l Hours (1-12)
%M month name
%m month, numeric (00-12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss AM or PM)
%S Seconds (00-59)
%s Seconds (00-59)
%T time, 24-hour (hh:mm:ss)
% U Week (00-53) Sunday is the first day of the week
% u Week (00-53) Monday is the first day of the week
%V Week (01-53) Sunday is the first day of the week, used with %X
%v Week (01-53) Monday is the first day of the week, used with %x
%W week name
%w day of the week (0=Sunday, 6=Saturday)
%X Year, where Sunday is the first day of the week, 4 digits, used with %V
%x Year, where Monday is the first day of the week, 4 digits, used with %v
%AND year, 4 digits
%and year, 2 digits

example:

1
2
3
4
DATE_FORMAT(NOW(), '%b %d %Y %h:%i %p' )
DATE_FORMAT(NOW(), '%m-%d-%Y' )
DATE_FORMAT(NOW(), '%d %b %y' )
DATE_FORMAT(NOW(), '%d %b %Y %T:%f' )

Output result:

?
1
2
3
4
Dec 29 2008 11:45 PM
12-29-2008
29 Dec 08
29 Dec 2008 16:25:46

2. MySQL format function FROM_UNIXTIME()

?
1
2
3
SELECT FROM_UNIXTIME( date , '%Y-%c-%d %h:%i:%s' ) as post_date ,
date_format(NOW(), '%Y-%c-%d %h:%i:%s' ) as post_date_gmt
FROM `article` where outkey = 'Y'

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326296782&siteId=291194637