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)
Specifier | Description |
---|---|
%a | Abbreviated weekday name (Sun ..Sat ) |
%b | Abbreviated month name (Jan ..Dec ) |
%c | Month, numeric (0 ..12 ) |
%D | Day of the month with English suffix (0th , 1st , 2nd , 3rd , …) |
%d | Day of the month, numeric (00 ..31 ) |
%e | Day of the month, numeric (0 ..31 ) |
%f | Microseconds (000000 ..999999 ) |
%H | Hour (00 ..23 ) |
%h | Hour (01 ..12 ) |
%I | Hour (01 ..12 ) |
%i | Minutes, numeric (00 ..59 ) |
%j | Day of year (001 ..366 ) |
%k | Hour (0 ..23 ) |
%l | Hour (1 ..12 ) |
%M | Month name (January ..December ) |
%m | Month, numeric (00 ..12 ) |
%p | AM or PM |
%r | Time, 12-hour (hh:mm:ss followed by AM or PM ) |
%S | Seconds (00 ..59 ) |
%s | Seconds (00 ..59 ) |
%T | Time, 24-hour (hh:mm:ss ) |
%U | Week (00 ..53 ), where Sunday is the first day of the week |
%u | Week (00 ..53 ), where Monday is the first day of the week |
%V | Week (01 ..53 ), where Sunday is the first day of the week; used with %X |
%v | Week (01 ..53 ), where Monday is the first day of the week; used with %x |
%W | Weekday name (Sunday ..Saturday ) |
%w | Day of the week (0 =Sunday..6 =Saturday) |
%X | Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
%x | Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
%Y | Year, numeric, four digits |
%y | Year, numeric (two digits) |
%% | A literal "% " character |
% | x , for any "x " not listed above |
说明符 | 描述 |
---|---|
%a | 简写的星期名( Sun .. Sat ) |
%b | 缩写的月份名称( Jan ... Dec ) |
%c | 月份,数字( 0 .. 12 ) |
%D | 带有英语后缀的月份中的一天( 0th , 1st , 2nd 日, 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 | AM 或PM |
%r | 时间(12小时)( hh:mm:ss 后跟AM 或PM ) |
%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 ” |
Be kind to your users and format the date for them!
善待您的用户并为他们设置日期格式!
所有评论(0)