lbmzorx 2018-06-02 17:28:15 6827次浏览 1条评论 0 3 0

formatter 是比较好用的格式化工具,在使用时候可以配合日期验证器使用。
日期的形式灵活多样,那么到底该怎么转换呢?

具体规则在 yii\helpers\BaseFormatConverterconvertDateIcuToPhp 方法内定义

规则为

"''" => "\\'",  // two single quotes produce one
'G' => '',      // era designator like (Anno Domini)
'Y' => 'o',     // 4digit year of "Week of Year"
'y' => 'Y',     // 4digit year e.g. 2014
'yyyy' => 'Y',  // 4digit year e.g. 2014
'yy' => 'y',    // 2digit year number eg. 14
'u' => '',      // extended year e.g. 4601
'U' => '',      // cyclic year name, as in Chinese lunar calendar
'r' => '',      // related Gregorian year e.g. 1996
'Q' => '',      // number of quarter
'QQ' => '',     // number of quarter '02'
'QQQ' => '',    // quarter 'Q2'
'QQQQ' => '',   // quarter '2nd quarter'
'QQQQQ' => '',  // number of quarter '2'
'q' => '',      // number of Stand Alone quarter
'qq' => '',     // number of Stand Alone quarter '02'
'qqq' => '',    // Stand Alone quarter 'Q2'
'qqqq' => '',   // Stand Alone quarter '2nd quarter'
'qqqqq' => '',  // number of Stand Alone quarter '2'
'M' => 'n',     // Numeric representation of a month, without leading zeros
'MM' => 'm',    // Numeric representation of a month, with leading zeros
'MMM' => 'M',   // A short textual representation of a month, three letters
'MMMM' => 'F',  // A full textual representation of a month, such as January or March
'MMMMM' => '',
'L' => 'n',     // Stand alone month in year
'LL' => 'm',    // Stand alone month in year
'LLL' => 'M',   // Stand alone month in year
'LLLL' => 'F',  // Stand alone month in year
'LLLLL' => '',  // Stand alone month in year
'w' => 'W',     // ISO-8601 week number of year
'ww' => 'W',    // ISO-8601 week number of year
'W' => '',      // week of the current month
'd' => 'j',     // day without leading zeros
'dd' => 'd',    // day with leading zeros
'D' => 'z',     // day of the year 0 to 365
'F' => '',      // Day of Week in Month. eg. 2nd Wednesday in July
'g' => '',      // Modified Julian day. This is different from the conventional Julian day number in two regards.
'E' => 'D',     // day of week written in short form eg. Sun
'EE' => 'D',
'EEE' => 'D',
'EEEE' => 'l',  // day of week fully written eg. Sunday
'EEEEE' => '',
'EEEEEE' => '',
'e' => 'N',     // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun
'ee' => 'N',    // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year
'eee' => 'D',
'eeee' => 'l',
'eeeee' => '',
'eeeeee' => '',
'c' => 'N',     // ISO-8601 numeric representation of the day of the week 1=Mon to 7=Sun
'cc' => 'N',    // php 'w' 0=Sun to 6=Sat isn't supported by ICU -> 'w' means week number of year
'ccc' => 'D',
'cccc' => 'l',
'ccccc' => '',
'cccccc' => '',
'a' => 'A',     // AM/PM marker
'h' => 'g',     // 12-hour format of an hour without leading zeros 1 to 12h
'hh' => 'h',    // 12-hour format of an hour with leading zeros, 01 to 12 h
'H' => 'G',     // 24-hour format of an hour without leading zeros 0 to 23h
'HH' => 'H',    // 24-hour format of an hour with leading zeros, 00 to 23 h
'k' => '',      // hour in day (1~24)
'kk' => '',     // hour in day (1~24)
'K' => '',      // hour in am/pm (0~11)
'KK' => '',     // hour in am/pm (0~11)
'm' => 'i',     // Minutes without leading zeros, not supported by php but we fallback
'mm' => 'i',    // Minutes with leading zeros
's' => 's',     // Seconds, without leading zeros, not supported by php but we fallback
'ss' => 's',    // Seconds, with leading zeros
'S' => '',      // fractional second
'SS' => '',     // fractional second
'SSS' => '',    // fractional second
'SSSS' => '',   // fractional second
'A' => '',      // milliseconds in day
'z' => 'T',     // Timezone abbreviation
'zz' => 'T',    // Timezone abbreviation
'zzz' => 'T',   // Timezone abbreviation
'zzzz' => 'T',  // Timezone full name, not supported by php but we fallback
'Z' => 'O',     // Difference to Greenwich time (GMT) in hours
'ZZ' => 'O',    // Difference to Greenwich time (GMT) in hours
'ZZZ' => 'O',   // Difference to Greenwich time (GMT) in hours
'ZZZZ' => '\G\M\TP', // Time Zone: long localized GMT (=OOOO) e.g. GMT-08:00
'ZZZZZ' => '',  //  TIme Zone: ISO8601 extended hms? (=XXXXX)
'O' => '',      // Time Zone: short localized GMT e.g. GMT-8
'OOOO' => '\G\M\TP', //  Time Zone: long localized GMT (=ZZZZ) e.g. GMT-08:00
'v' => '\G\M\TP', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here
'vvvv' => '\G\M\TP', // Time Zone: generic non-location (falls back first to VVVV and then to OOOO) using the ICU defined fallback here
'V' => '',      // Time Zone: short time zone ID
'VV' => 'e',    // Time Zone: long time zone ID
'VVV' => '',    // Time Zone: time zone exemplar city
'VVVV' => '\G\M\TP', // Time Zone: generic location (falls back to OOOO) using the ICU defined fallback here
'X' => '',      // Time Zone: ISO8601 basic hm?, with Z for 0, e.g. -08, +0530, Z
'XX' => 'O, \Z', // Time Zone: ISO8601 basic hm, with Z, e.g. -0800, Z
'XXX' => 'P, \Z',    // Time Zone: ISO8601 extended hm, with Z, e.g. -08:00, Z
'XXXX' => '',   // Time Zone: ISO8601 basic hms?, with Z, e.g. -0800, -075258, Z
'XXXXX' => '',  // Time Zone: ISO8601 extended hms?, with Z, e.g. -08:00, -07:52:58, Z
'x' => '',      // Time Zone: ISO8601 basic hm?, without Z for 0, e.g. -08, +0530
'xx' => 'O',    // Time Zone: ISO8601 basic hm, without Z, e.g. -0800
'xxx' => 'P',   // Time Zone: ISO8601 extended hm, without Z, e.g. -08:00
'xxxx' => '',   // Time Zone: ISO8601 basic hms?, without Z, e.g. -0800, -075258
'xxxxx' => '',  // Time Zone: ISO8601 extended hms?, without Z, e.g. -08:00, -07:52:58           

数组的键为yii使用的格式,第二个参数为对应到date() 函数使用的格式
常用的对应 如'yyyy-MM-dd HH🇲🇲ss' 对应 Y-m-d H:i:s,可以按图索骥。

觉得很赞
您需要登录后才可以评论。登录 | 立即注册