For formatting of outputs Yii provides a formatter class to make data more readable for users.
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](concept-components.md) named `formatter` by default.
[[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.
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.
The formatter can be used in two different ways.
...
@@ -31,7 +31,7 @@ The formatter can be used in two different ways.
...
@@ -31,7 +31,7 @@ The formatter can be used in two different ways.
> is the same. See also: [Setting up your PHP environment for internationalization](tutorial-i18n.md#setup-environment).
> is the same. See also: [Setting up your PHP environment for internationalization](tutorial-i18n.md#setup-environment).
Configuring the format
Configuring the formatter <a name="configuring-format"></a>
----------------------
-------------------------
The default format of the Formatter class can be adjusted using the properties 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).
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.
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]].
For more details about the available properties check out the [[yii\i18n\Formatter|API documentation of the Formatter class]] and the following subsections.
```php
```php
'components' => [
'components' => [
...
@@ -72,63 +72,112 @@ For more details about the available properties check out the [[yii\i18n\Formatt
...
@@ -72,63 +72,112 @@ For more details about the available properties check out the [[yii\i18n\Formatt
],
],
```
```
Formatting Dates
Formatting Date and Time values <a name="date-and-time"></a>
----------------
-------------------------------
> Note: This section is under development.
The formatter class provides different methods for formatting date and time values. These are:
TDB
- [[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
and now in human readable form e.g. `1 hour ago`.
See http://site.icu-project.org/ for the format.
The date and time format for the [[yii\i18n\Formatter::asDate()|date]], [[yii\i18n\Formatter::asTime()|time]], and
[[yii\i18n\Formatter::asDatetime()|datetime]] method can be specified globally by configuring the formatters
properties [[yii\i18n\Formatter::$dateFormat|$dateFormat]], [[yii\i18n\Formatter::$timeFormat|$timeFormat]], and
@@ -63,7 +63,7 @@ severity levels and categories and then exports them to some medium. For example
...
@@ -63,7 +63,7 @@ severity levels and categories and then exports them to some medium. For example
exports the filtered log messages to a database table, while a [[yii\log\EmailTarget|email target]] exports
exports the filtered log messages to a database table, while a [[yii\log\EmailTarget|email target]] exports
the log messages to specified email addresses.
the log messages to specified email addresses.
You can register multiple log targets in an application by configuring them through the `log`application component
You can register multiple log targets in an application by configuring them through the `log`[application component](structure-application-components.md)
in the application configuration, like the following:
in the application configuration, like the following:
As can be seen in the code above, the console application uses its own configuration file, named `console.php`. In this file
As can be seen in the code above, the console application uses its own configuration file, named `console.php`. In this file
you should configure various application components and properties for the console application in particular.
you should configure various [application components](structure-application-components.md) and properties for the console application in particular.
If your web application and the console application share a lot of configuration parameters and values, you may consider moving the common
If your web application and the console application share a lot of configuration parameters and values, you may consider moving the common
parts into a separate file, and including this file in both of the application configurations (web and console). You can see an example of this in the "advanced" application template.
parts into a separate file, and including this file in both of the application configurations (web and console). You can see an example of this in the "advanced" application template.