For formatting of outputs Yii provides a formatter class to make data more readable for users.
[[yii\i18n\Formatter]] is a helper class that is registered as an [application component](structure-application-components.md) named `formatter` by default.
它提供一些方法用来格式化数据,如日期/时间、数字或其他常用的本地化格式,
两种方式使用格式器:
It provides a set of methods for data formatting purpose such as date/time values, numbers and other commonly used formats in a localized way.
The formatter can be used in two different ways.
1. 直接使用格式化方法(所有的格式器方法以 `as`做前缀):
1. Using the formatting methods (all formatter methods prefixed with `as`) directly:
```php
echoYii::$app->formatter->asDate('2014-01-01','long');// output: January 1, 2014
更多关于可用属性的详情请参考 [[yii\i18n\Formatter|API documentation of the Formatter class]] 和接下来一小节。
The default formats used by the formatter methods can be adjusted using the properties of the [[yii\i18n\Formatter|formatter class]].
You can adjust these values application wide by configuring the `formatter` component in your [application config](concept-configurations.md#application-configurations).
An example configuration is shown in the following.
...
...
@@ -72,11 +91,18 @@ For more details about the available properties check out the [[yii\i18n\Formatt
],
```
格式化日期和时间 <a name="date-and-time"></a>
Formatting Date and Time values <a name="date-and-time"></a>
-------------------------------
格式器类为格式化日期和时间提供了多个方法:
The formatter class provides different methods for formatting date and time values. These are: