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');// 输出: January 1, 2014
...
...
@@ -39,12 +34,6 @@ The formatter can be used in two different ways.
更多关于可用属性的详情请参考 [[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.
For more details about the available properties check out the [[yii\i18n\Formatter|API documentation of the Formatter class]] and the following subsections.
```php
'components' => [
...
...
@@ -92,7 +72,6 @@ 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>
-------------------------------
格式器类为格式化日期和时间提供了多个方法:
...
...
@@ -103,27 +82,14 @@ The formatter class provides different methods for formatting date and time valu
- [[yii\i18n\Formatter::asDate()|date]] - the value is formatted as a date e.g. `January, 01 2014`.
- [[yii\i18n\Formatter::asTime()|time]] - the value is formatted as a time e.g. `14:23`.
- [[yii\i18n\Formatter::asDatetime()|datetime]] - the value is formatted as date and time e.g. `January, 01 2014 14:23`.
- [[yii\i18n\Formatter::asTimestamp()|timestamp]] - the value is formatted as a [unix timestamp](http://en.wikipedia.org/wiki/Unix_time) e.g. `1412609982`.
- [[yii\i18n\Formatter::asRelativeTime()|relativeTime]] - the value is formatted as the time interval between a date