Yii2 日期格式从 2 位数年份到 4 位数
Yii2 date format from 2 digit year to 4 digit
我有一个这样的日期:17-05-17
,我想这样显示它:2017-05-17
。
\Yii::$app->formatter->asDate('17-05-17')
\Yii::$app->formatter->asDate('17-05-17', 'php:Y-m-d');
两者都显示 0017-05-17
。你能给我指出正确的方向吗?
使用这些:
Yii::$app->formatter->asDate('17-05-17', 'php:Y-m-d');
参考:https://www.yiiframework.com/doc/guide/2.0/en/output-formatting
您可以在组件中为 Class yii\i18n\Formatter
配置 dateFormat
属性 或通过将参数传递给函数
$dateFormat
public property The default format string to be used to
format a date. This can be "short", "medium", "long", or "full", which
represents a preset format of different lengths.
It can also be a custom format as specified in the ICU manual.
Alternatively, this can be a string prefixed with php: representing a
format that can be recognized by the PHP date()-function.
在配置中
'components'=>[
'formatter'=>[
'dateFormat'=>'yyyy/MM/dd' //ICU FORMAT
//or php format
//'dateFormat'=>'php:y/m/d' //php FORMAT
]
]
并像
一样使用它
Yii::$app->formatter->asDate($date);
或者传入参数
Yii::$app->formatter->asDate($date,'yyyy/MM/dd');
您需要使用小写 y
来解析 2 位数年份的日期:
$parsed = \DateTime::createFromFormat('y-m-d', '17-05-17');
$formatted = \Yii::$app->formatter->asDate($parsed, 'php:Y-m-d');
我有一个这样的日期:17-05-17
,我想这样显示它:2017-05-17
。
\Yii::$app->formatter->asDate('17-05-17')
\Yii::$app->formatter->asDate('17-05-17', 'php:Y-m-d');
两者都显示 0017-05-17
。你能给我指出正确的方向吗?
使用这些:
Yii::$app->formatter->asDate('17-05-17', 'php:Y-m-d');
参考:https://www.yiiframework.com/doc/guide/2.0/en/output-formatting
您可以在组件中为 Class yii\i18n\Formatter
配置 dateFormat
属性 或通过将参数传递给函数
$dateFormat
public property The default format string to be used to format a date. This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.It can also be a custom format as specified in the ICU manual. Alternatively, this can be a string prefixed with php: representing a format that can be recognized by the PHP date()-function.
在配置中
'components'=>[
'formatter'=>[
'dateFormat'=>'yyyy/MM/dd' //ICU FORMAT
//or php format
//'dateFormat'=>'php:y/m/d' //php FORMAT
]
]
并像
一样使用它Yii::$app->formatter->asDate($date);
或者传入参数
Yii::$app->formatter->asDate($date,'yyyy/MM/dd');
您需要使用小写 y
来解析 2 位数年份的日期:
$parsed = \DateTime::createFromFormat('y-m-d', '17-05-17');
$formatted = \Yii::$app->formatter->asDate($parsed, 'php:Y-m-d');