I use MySQL DATE and DATETIME fields almost as much as VARCHAR fields. I use them mostly to track record creation, record updates, and record expiration dates. The default date format in MYSQL is:

我使用MySQL DATE和DATETIME字段几乎与VARCHAR字段一样多。 我主要使用它们来跟踪记录创建,记录更新和记录到期日期。 MYSQL中的默认日期格式为:


2008-09-10 22:50:02


When I pull this information into the page, I want to show the date in human-readable format. That's where MySQL's DATE_FORMAT functionality comes in. DATE_FORMAT allows me to format the date in any manner you'd like.

当我将此信息拉入页面时,我想以易于理解的格式显示日期。 这就是MySQL的DATE_FORMAT功能所在的地方。DATE_FORMAT允许我以您想要的任何方式格式化日期。

MySQL DATE_FORMAT()示例 (MySQL DATE_FORMAT() Example)


DATE_FORMAT(NOW(),'%W, %M %e, %Y @ %h:%i %p')
#yields 'Sunday, September 20, 2008 @ 12:45 PM'


MySQL DATE_FORMAT()字母表示 (MySQL DATE_FORMAT() Letter Representations)

SpecifierDescription
%aAbbreviated weekday name (Sun..Sat)
%bAbbreviated month name (Jan..Dec)
%cMonth, numeric (0..12)
%DDay of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%dDay of the month, numeric (00..31)
%eDay of the month, numeric (0..31)
%fMicroseconds (000000..999999)
%HHour (00..23)
%hHour (01..12)
%IHour (01..12)
%iMinutes, numeric (00..59)
%jDay of year (001..366)
%kHour (0..23)
%lHour (1..12)
%MMonth name (January..December)
%mMonth, numeric (00..12)
%p AM or PM
%rTime, 12-hour (hh:mm:ss followed by AM or PM)
%SSeconds (00..59)
%sSeconds (00..59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00..53), where Sunday is the first day of the week
%uWeek (00..53), where Monday is the first day of the week
%VWeek (01..53), where Sunday is the first day of the week; used with %X
%vWeek (01..53), where Monday is the first day of the week; used with %x
%WWeekday name (Sunday..Saturday)
%wDay of the week (0=Sunday..6=Saturday)
%XYear for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%xYear for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric (two digits)
%%A literal "%" character
%x x, for any "x" not listed above
说明符 描述
%a 简写的星期名( Sun .. Sat )
%b 缩写的月份名称( Jan ... Dec )
%c 月份,数字( 0 .. 12 )
%D 带有英语后缀的月份中的一天( 0th1st2nd日, 3rd ,...)
%d 每月的某天,数字( 00 .. 31 )
%e 每月的某天,数字( 0 .. 31 )
%f 微秒( 000000 .. 999999 )
%H 小时( 00 .. 23 )
%h 小时( 01 .. 12 )
%I 小时( 01 .. 12 )
%i 分钟,数字( 00 .. 59 )
%j 一年中的001 ( 001 .. 366 )
%k 小时( 0 .. 23 )
%l 小时( 1 .. 12 )
%M 月名称( January .. December )
%m 月份,数字( 00 .. 12 )
%p AMPM
%r 时间(12小时)( hh:mm:ss后跟AMPM )
%S 秒( 00 .. 59 )
%s 秒( 00 .. 59 )
%T 时间24小时( hh:mm:ss )
%U 周( 00 .. 53 ),其中星期日是一周的第一天
%u 周( 00 .. 53 ),其中星期一是一周的第一天
%V 周( 01 .. 53 ),其中星期日是一周的第一天; 与%X
%v 周( 01 .. 53 ),其中星期一是一周的第一天; 与%x
%W 工作日名称( Sunday .. Saturday )
%w 星期几( 0 =星期日.. 6 =星期六)
%X 星期的年份,其中星期日是一周的第一天,数字,四位数; 与%V
%x 一周的年份,其中星期一是一周的第一天,数字,四位数; 与%v
%Y 年,数字,四位数
%y 年,数字(两位数字)
%% 文字“ % ”字符
% x x ,对于上面未列出的任何“ x

Be kind to your users and format the date for them!

善待您的用户并为他们设置日期格式!

翻译自: https://davidwalsh.name/format-date-mysql-date_format

Logo

更多推荐