Class yii\i18n\Formatter
Inheritance | yii\i18n\Formatter » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/i18n/Formatter.php |
Formatter provides a set of commonly used data formatting methods.
The formatting methods provided by Formatter are all named in the form of asXyz()
.
The behavior of some of them may be configured via the properties of Formatter. For example,
by configuring $dateFormat, one may control how asDate() formats the value into a date string.
Formatter is configured as an application component in yii\base\Application by default.
You can access that instance via Yii::$app->formatter
.
The Formatter class is designed to format values according to a $locale. For this feature to work the PHP intl extension has to be installed. Most of the methods however work also if the PHP intl extension is not installed by providing a fallback implementation. Without intl month and day names are in English only. Note that even if the intl extension is installed, formatting date and time values for years >=2038 or <=1901 on 32bit systems will fall back to the PHP implementation because intl uses a 32bit UNIX timestamp internally. On a 64bit system the intl formatter is used in all cases if installed.
Note: The Formatter class is meant to be used for formatting values for display to users in different languages and time zones. If you need to format a date or time in machine readable format, use the PHP date() function instead.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$baseUnits | array | The base units that are used as multipliers for smallest possible unit from $measureUnits. | yii\i18n\Formatter |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$booleanFormat | array | The text to be displayed when formatting a boolean value. | yii\i18n\Formatter |
$calendar | IntlCalendar|integer|null | The calendar to be used for date formatting. | yii\i18n\Formatter |
$currencyCode | string | The 3-letter ISO 4217 currency code indicating the default currency to use for asCurrency(). | yii\i18n\Formatter |
$dateFormat | string | The default format string to be used to format a date. | yii\i18n\Formatter |
$datetimeFormat | string | The default format string to be used to format a date and time. | yii\i18n\Formatter |
$decimalSeparator | string | The character displayed as the decimal point when formatting a number. | yii\i18n\Formatter |
$defaultTimeZone | string | The time zone that is assumed for input values if they do not include a time zone explicitly. | yii\i18n\Formatter |
$locale | string | The locale ID that is used to localize the date and number formatting. | yii\i18n\Formatter |
$measureUnits | array | Configuration of weight and length measurement units. | yii\i18n\Formatter |
$nullDisplay | string | The text to be displayed when formatting a null value. |
yii\i18n\Formatter |
$numberFormatterOptions | array | A list of name value pairs that are passed to the intl [NumberFormatter::setAttribute()](https://secure. | yii\i18n\Formatter |
$numberFormatterSymbols | array | A list of name value pairs that are passed to the intl [NumberFormatter::setSymbol()](https://secure. | yii\i18n\Formatter |
$numberFormatterTextOptions | array | A list of name value pairs that are passed to the intl [NumberFormatter::setTextAttribute()](https://secure. | yii\i18n\Formatter |
$sizeFormatBase | integer | The base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte), used by asSize() and asShortSize(). | yii\i18n\Formatter |
$systemOfUnits | string | Default system of measure units. | yii\i18n\Formatter |
$thousandSeparator | string | The character displayed as the thousands separator (also called grouping separator) character when formatting a number. | yii\i18n\Formatter |
$timeFormat | string | The default format string to be used to format a time. | yii\i18n\Formatter |
$timeZone | string | The time zone to use for formatting time and date values. | yii\i18n\Formatter |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\BaseObject |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
asBoolean() | Formats the value as a boolean. | yii\i18n\Formatter |
asCurrency() | Formats the value as a currency number. | yii\i18n\Formatter |
asDate() | Formats the value as a date. | yii\i18n\Formatter |
asDatetime() | Formats the value as a datetime. | yii\i18n\Formatter |
asDecimal() | Formats the value as a decimal number. | yii\i18n\Formatter |
asDuration() | Represents the value as duration in human readable format. | yii\i18n\Formatter |
asEmail() | Formats the value as a mailto link. | yii\i18n\Formatter |
asHtml() | Formats the value as HTML text. | yii\i18n\Formatter |
asImage() | Formats the value as an image tag. | yii\i18n\Formatter |
asInteger() | Formats the value as an integer number by removing any decimal digits without rounding. | yii\i18n\Formatter |
asLength() | Formats the value as a length in human readable form for example 12 meters . |
yii\i18n\Formatter |
asNtext() | Formats the value as an HTML-encoded plain text with newlines converted into breaks. | yii\i18n\Formatter |
asOrdinal() | Formats the value as a ordinal value of a number. | yii\i18n\Formatter |
asParagraphs() | Formats the value as HTML-encoded text paragraphs. | yii\i18n\Formatter |
asPercent() | Formats the value as a percent number with "%" sign. | yii\i18n\Formatter |
asRaw() | Formats the value as is without any formatting. | yii\i18n\Formatter |
asRelativeTime() | Formats the value as the time interval between a date and now in human readable form. | yii\i18n\Formatter |
asScientific() | Formats the value as a scientific number. | yii\i18n\Formatter |
asShortLength() | Formats the value as a length in human readable form for example 12 m . |
yii\i18n\Formatter |
asShortSize() | Formats the value in bytes as a size in human readable form for example 12 kB . |
yii\i18n\Formatter |
asShortWeight() | Formats the value as a weight in human readable form for example 12 kg . |
yii\i18n\Formatter |
asSize() | Formats the value in bytes as a size in human readable form, for example 12 kilobytes . |
yii\i18n\Formatter |
asSpellout() | Formats the value as a number spellout. | yii\i18n\Formatter |
asText() | Formats the value as an HTML-encoded plain text. | yii\i18n\Formatter |
asTime() | Formats the value as a time. | yii\i18n\Formatter |
asTimestamp() | Formats a date, time or datetime in a float number as UNIX timestamp (seconds since 01-01-1970). | yii\i18n\Formatter |
asUrl() | Formats the value as a hyperlink. | yii\i18n\Formatter |
asWeight() | Formats the value as a weight in human readable form for example 12 kilograms . |
yii\i18n\Formatter |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
format() | Formats the value based on the given format type. | yii\i18n\Formatter |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
init() | Initializes the object. | yii\i18n\Formatter |
off() | Detaches an existing event handler from this component. | yii\base\Component |
on() | Attaches an event handler to an event. | yii\base\Component |
trigger() | Triggers an event. | yii\base\Component |
Protected Methods
Method | Description | Defined By |
---|---|---|
asCurrencyStringFallback() | Fallback for formatting value as a currency number. | yii\i18n\Formatter |
asDecimalStringFallback() | Fallback for formatting value as a decimal number. | yii\i18n\Formatter |
asIntegerStringFallback() | Fallback for formatting value as an integer number by removing any decimal digits without rounding. | yii\i18n\Formatter |
asPercentStringFallback() | Fallback for formatting value as a percent number with "%" sign. | yii\i18n\Formatter |
createNumberFormatter() | Creates a number formatter based on the given type and format. | yii\i18n\Formatter |
isNormalizedValueMispresented() | Checks if string representations of given value and its normalized version are different. | yii\i18n\Formatter |
normalizeDatetimeValue() | Normalizes the given datetime value as a DateTime object that can be taken by various date/time formatting methods. | yii\i18n\Formatter |
normalizeNumericStringValue() | Normalizes a numeric string value. | yii\i18n\Formatter |
normalizeNumericValue() | Normalizes a numeric input value. | yii\i18n\Formatter |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
FORMAT_WIDTH_LONG | 'long' | yii\i18n\Formatter | |
FORMAT_WIDTH_SHORT | 'short' | yii\i18n\Formatter | |
UNIT_LENGTH | 'length' | yii\i18n\Formatter | |
UNIT_SYSTEM_IMPERIAL | 'imperial' | yii\i18n\Formatter | |
UNIT_SYSTEM_METRIC | 'metric' | yii\i18n\Formatter | |
UNIT_WEIGHT | 'mass' | yii\i18n\Formatter |
Property Details
The base units that are used as multipliers for smallest possible unit from $measureUnits.
The text to be displayed when formatting a boolean value. The first element corresponds
to the text displayed for false
, the second element for true
.
Defaults to ['No', 'Yes']
, where Yes
and No
will be translated according to $locale.
The calendar to be used for date formatting. The value of this property will be directly
passed to the constructor of the IntlDateFormatter
class.
Defaults to null
, which means the Gregorian calendar will be used. You may also explicitly pass the constant
\IntlDateFormatter::GREGORIAN
for Gregorian calendar.
To use an alternative calendar like for example the Jalali calendar,
set this property to \IntlDateFormatter::TRADITIONAL
.
The calendar must then be specified in the $locale, for example for the persian calendar the configuration for the formatter would be:
'formatter' => [
'locale' => 'fa_IR@calendar=persian',
'calendar' => \IntlDateFormatter::TRADITIONAL,
],
Available calendar names can be found in the ICU manual.
Since PHP 5.5 you may also use an instance of the IntlCalendar class. Check the PHP manual for more details.
If the PHP intl extension is not available, setting this property will have no effect.
See also:
The 3-letter ISO 4217 currency code indicating the default currency to use for asCurrency().
If not set, the currency code corresponding to $locale will be used.
Note that in this case the $locale has to be specified with a country code, e.g. en-US
otherwise it
is not possible to determine the default currency.
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.
For example:
'MM/dd/yyyy' // date in ICU format
'php:m/d/Y' // the same date in PHP format
The default format string to be used to format a date and time. 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.
For example:
'MM/dd/yyyy HH:mm:ss' // date and time in ICU format
'php:m/d/Y H:i:s' // the same date and time in PHP format
The character displayed as the decimal point when formatting a number. If not set, the decimal separator corresponding to $locale will be used. If PHP intl extension is not available, the default value is '.'.
The time zone that is assumed for input values if they do not include a time zone explicitly.
The value must be a valid time zone identifier, e.g. UTC
, Europe/Berlin
or America/Chicago
.
Please refer to the php manual for available time zones.
It defaults to UTC
so you only have to adjust this value if you store datetime values in another time zone in your database.
Note that a UNIX timestamp is always in UTC by its definition. That means that specifying a default time zone different from UTC has no effect on date values given as UNIX timestamp.
The locale ID that is used to localize the date and number formatting. For number and date formatting this is only effective when the PHP intl extension is installed. If not set, yii\base\Application::$language will be used.
Configuration of weight and length measurement units. This array contains the most usable measurement units, but you can change it in case you have some special requirements.
For example, you can add smaller measure unit:
$this->measureUnits[self::UNIT_LENGTH][self::UNIT_SYSTEM_METRIC] = [
'nanometer' => 0.000001
]
See also:
The text to be displayed when formatting a null
value.
Defaults to '<span class="not-set">(not set)</span>'
, where (not set)
will be translated according to $locale.
A list of name value pairs that are passed to the intl NumberFormatter::setAttribute() method of all the number formatter objects created by createNumberFormatter(). This property takes only effect if the PHP intl extension is installed.
Please refer to the PHP manual for the possible options.
For example to adjust the maximum and minimum value of fraction digits you can configure this property like the following:
[
NumberFormatter::MIN_FRACTION_DIGITS => 0,
NumberFormatter::MAX_FRACTION_DIGITS => 2,
]
A list of name value pairs that are passed to the intl NumberFormatter::setSymbol() method of all the number formatter objects created by createNumberFormatter(). This property takes only effect if the PHP intl extension is installed.
Please refer to the PHP manual for the possible options.
For example to choose a custom currency symbol, e.g. U+20BD instead of руб.
for Russian Ruble:
[
NumberFormatter::CURRENCY_SYMBOL => '₽',
]
A list of name value pairs that are passed to the intl NumberFormatter::setTextAttribute() method of all the number formatter objects created by createNumberFormatter(). This property takes only effect if the PHP intl extension is installed.
Please refer to the PHP manual for the possible options.
For example to change the minus sign for negative numbers you can configure this property like the following:
[
NumberFormatter::NEGATIVE_PREFIX => 'MINUS',
]
The base at which a kilobyte is calculated (1000 or 1024 bytes per kilobyte), used by asSize() and asShortSize(). Defaults to 1024.
Default system of measure units. Defaults to UNIT_SYSTEM_METRIC. Possible values:
See also:
The character displayed as the thousands separator (also called grouping separator) character when formatting a number. If not set, the thousand separator corresponding to $locale will be used. If PHP intl extension is not available, the default value is ','.
The default format string to be used to format a time. 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.
For example:
'HH:mm:ss' // time in ICU format
'php:H:i:s' // the same time in PHP format
The time zone to use for formatting time and date values.
This can be any value that may be passed to date_default_timezone_set()
e.g. UTC
, Europe/Berlin
or America/Chicago
.
Refer to the php manual for available time zones.
If this property is not set, yii\base\Application::$timeZone will be used.
Note that the default time zone for input data is assumed to be UTC by default if no time zone is included in the input date value. If you store your data in a different time zone in the database, you have to adjust $defaultTimeZone accordingly.
Method Details
Formats the value as a boolean.
See also $booleanFormat.
public string asBoolean ( $value ) | ||
$value | mixed | The value to be formatted. |
return | string | The formatted result. |
---|
Formats the value as a currency number.
This function does not require the PHP intl extension to be installed to work, but it is highly recommended to install it to get good formatting results.
Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function without PHP intl extension support. For very big numbers it's recommended to pass them as strings and not use scientific notation otherwise the output might be wrong.
public string asCurrency ( $value, $currency = null, $options = [], $textOptions = [] ) | ||
$value | mixed | The value to be formatted. |
$currency | string | The 3-letter ISO 4217 currency code indicating the currency to use. If null, $currencyCode will be used. |
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
throws | yii\base\InvalidConfigException | if no currency is given and $currencyCode is not defined. |
Fallback for formatting value as a currency number.
protected string asCurrencyStringFallback ( $value, $currency = null ) | ||
$value | string|integer|float | The value to be formatted. |
$currency | string | The 3-letter ISO 4217 currency code indicating the currency to use. If null, $currencyCode will be used. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidConfigException | if no currency is given and $currencyCode is not defined. |
Formats the value as a date.
See also $dateFormat.
public string asDate ( $value, $format = null ) | ||
$value | integer|string|DateTime | The value to be formatted. The following types of value are supported:
The formatter will convert date values according to $timeZone before formatting it.
If no timezone conversion should be performed, you need to set $defaultTimeZone and $timeZone to the same value.
Also no conversion will be performed on values that have no time information, e.g. |
$format | string | The format used to convert the value into a date string. If null, $dateFormat will be used. 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 |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value can not be evaluated as a date value. |
throws | yii\base\InvalidConfigException | if the date format is invalid. |
Formats the value as a datetime.
See also $datetimeFormat.
public string asDatetime ( $value, $format = null ) | ||
$value | integer|string|DateTime | The value to be formatted. The following types of value are supported:
The formatter will convert date values according to $timeZone before formatting it. If no timezone conversion should be performed, you need to set $defaultTimeZone and $timeZone to the same value. |
$format | string | The format used to convert the value into a date string. If null, $datetimeFormat will be used. 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 |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value can not be evaluated as a date value. |
throws | yii\base\InvalidConfigException | if the date format is invalid. |
Formats the value as a decimal number.
Property $decimalSeparator will be used to represent the decimal point. The value is rounded automatically to the defined decimal digits.
Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function without PHP intl extension support. For very big numbers it's recommended to pass them as strings and not use scientific notation otherwise the output might be wrong.
See also:
public string asDecimal ( $value, $decimals = null, $options = [], $textOptions = [] ) | ||
$value | mixed | The value to be formatted. |
$decimals | integer | The number of digits after the decimal point.
If not given, the number of digits depends in the input value and is determined based on
|
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
Fallback for formatting value as a decimal number.
Property $decimalSeparator will be used to represent the decimal point. The value is rounded automatically to the defined decimal digits.
See also:
protected string asDecimalStringFallback ( $value, $decimals = 2 ) | ||
$value | string|integer|float | The value to be formatted. |
$decimals | integer | The number of digits after the decimal point. The default value is |
return | string | The formatted result. |
---|
Represents the value as duration in human readable format.
public string asDuration ( $value, $implodeString = ', ', $negativeSign = '-' ) | ||
$value | DateInterval|string|integer | The value to be formatted. Acceptable formats:
|
$implodeString | string | Will be used to concatenate duration parts. Defaults to |
$negativeSign | string | Will be prefixed to the formatted duration, when it is negative. Defaults to |
return | string | The formatted duration. |
---|
Formats the value as a mailto link.
public string asEmail ( $value, $options = [] ) | ||
$value | string | The value to be formatted. |
$options | array | The tag options in terms of name-value pairs. See yii\helpers\Html::mailto(). |
return | string | The formatted result. |
---|
Formats the value as HTML text.
The value will be purified using yii\helpers\HtmlPurifier to avoid XSS attacks. Use asRaw() if you do not want any purification of the value.
public string asHtml ( $value, $config = null ) | ||
$value | string | The value to be formatted. |
$config | array|null | The configuration for the HTMLPurifier class. |
return | string | The formatted result. |
---|
Formats the value as an image tag.
public string asImage ( $value, $options = [] ) | ||
$value | mixed | The value to be formatted. |
$options | array | The tag options in terms of name-value pairs. See yii\helpers\Html::img(). |
return | string | The formatted result. |
---|
Formats the value as an integer number by removing any decimal digits without rounding.
Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function without PHP intl extension support. For very big numbers it's recommended to pass them as strings and not use scientific notation otherwise the output might be wrong.
public string asInteger ( $value, $options = [], $textOptions = [] ) | ||
$value | mixed | The value to be formatted. |
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
Fallback for formatting value as an integer number by removing any decimal digits without rounding.
protected string asIntegerStringFallback ( $value ) | ||
$value | string|integer|float | The value to be formatted. |
return | string | The formatted result. |
---|
Formats the value as a length in human readable form for example 12 meters
.
Check properties $baseUnits if you need to change unit of value as the multiplier of the smallest unit and $systemOfUnits to switch between UNIT_SYSTEM_METRIC or UNIT_SYSTEM_IMPERIAL.
See also asLength().
public string asLength ( $value, $decimals = null, $numberOptions = [], $textOptions = [] ) | ||
$value | float|integer | Value to be formatted. |
$decimals | integer | The number of digits after the decimal point. |
$numberOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
throws | yii\base\InvalidConfigException | when INTL is not installed or does not contain required information. |
Formats the value as an HTML-encoded plain text with newlines converted into breaks.
public string asNtext ( $value ) | ||
$value | string | The value to be formatted. |
return | string | The formatted result. |
---|
Formats the value as a ordinal value of a number.
This function requires the PHP intl extension to be installed.
This formatter does not work well with very big numbers.
public string asOrdinal ( $value ) | ||
$value | mixed | The value to be formatted |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
throws | yii\base\InvalidConfigException | when the PHP intl extension is not available. |
Formats the value as HTML-encoded text paragraphs.
Each text paragraph is enclosed within a <p>
tag.
One or multiple consecutive empty lines divide two paragraphs.
public string asParagraphs ( $value ) | ||
$value | string | The value to be formatted. |
return | string | The formatted result. |
---|
Formats the value as a percent number with "%" sign.
Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function without PHP intl extension support. For very big numbers it's recommended to pass them as strings and not use scientific notation otherwise the output might be wrong.
public string asPercent ( $value, $decimals = null, $options = [], $textOptions = [] ) | ||
$value | mixed | The value to be formatted. It must be a factor e.g. |
$decimals | integer | The number of digits after the decimal point.
If not given, the number of digits depends in the input value and is determined based on
|
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
Fallback for formatting value as a percent number with "%" sign.
Property $decimalSeparator will be used to represent the decimal point. The value is rounded automatically to the defined decimal digits.
protected string asPercentStringFallback ( $value, $decimals = null ) | ||
$value | string|integer|float | The value to be formatted. |
$decimals | integer | The number of digits after the decimal point. The default value is |
return | string | The formatted result. |
---|
Formats the value as is without any formatting.
This method simply returns back the parameter without any format.
The only exception is a null
value which will be formatted using $nullDisplay.
public string asRaw ( $value ) | ||
$value | mixed | The value to be formatted. |
return | string | The formatted result. |
---|
Formats the value as the time interval between a date and now in human readable form.
This method can be used in three different ways:
- Using a timestamp that is relative to
now
. - Using a timestamp that is relative to the
$referenceTime
. - Using a
DateInterval
object.
public string asRelativeTime ( $value, $referenceTime = null ) | ||
$value | integer|string|DateTime|DateInterval | The value to be formatted. The following types of value are supported:
|
$referenceTime | integer|string|DateTime | If specified the value is used as a reference time instead of |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value can not be evaluated as a date value. |
Formats the value as a scientific number.
public string asScientific ( $value, $decimals = null, $options = [], $textOptions = [] ) | ||
$value | mixed | The value to be formatted. |
$decimals | integer | The number of digits after the decimal point.
If not given, the number of digits depends in the input value and is determined based on
|
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
Formats the value as a length in human readable form for example 12 m
.
This is the short form of asLength().
Check properties $baseUnits if you need to change unit of value as the multiplier of the smallest unit and $systemOfUnits to switch between UNIT_SYSTEM_METRIC or UNIT_SYSTEM_IMPERIAL.
See also asLength().
public string asShortLength ( $value, $decimals = null, $options = [], $textOptions = [] ) | ||
$value | float|integer | Value to be formatted. |
$decimals | integer | The number of digits after the decimal point. |
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
throws | yii\base\InvalidConfigException | when INTL is not installed or does not contain required information. |
Formats the value in bytes as a size in human readable form for example 12 kB
.
This is the short form of asSize().
If $sizeFormatBase is 1024, binary prefixes (e.g. kibibyte/KiB, mebibyte/MiB, ...) are used in the formatting result.
See also:
public string asShortSize ( $value, $decimals = null, $options = [], $textOptions = [] ) | ||
$value | string|integer|float | Value in bytes to be formatted. |
$decimals | integer | The number of digits after the decimal point. |
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
Formats the value as a weight in human readable form for example 12 kg
.
This is the short form of asWeight().
Check properties $baseUnits if you need to change unit of value as the multiplier of the smallest unit and $systemOfUnits to switch between UNIT_SYSTEM_METRIC or UNIT_SYSTEM_IMPERIAL.
public string asShortWeight ( $value, $decimals = null, $options = [], $textOptions = [] ) | ||
$value | float|integer | Value to be formatted. |
$decimals | integer | The number of digits after the decimal point. |
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
throws | yii\base\InvalidConfigException | when INTL is not installed or does not contain required information. |
Formats the value in bytes as a size in human readable form, for example 12 kilobytes
.
If $sizeFormatBase is 1024, binary prefixes (e.g. kibibyte/KiB, mebibyte/MiB, ...) are used in the formatting result.
See also:
public string asSize ( $value, $decimals = null, $options = [], $textOptions = [] ) | ||
$value | string|integer|float | Value in bytes to be formatted. |
$decimals | integer | The number of digits after the decimal point. |
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
Formats the value as a number spellout.
This function requires the PHP intl extension to be installed.
This formatter does not work well with very big numbers.
public string asSpellout ( $value ) | ||
$value | mixed | The value to be formatted |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
throws | yii\base\InvalidConfigException | when the PHP intl extension is not available. |
Formats the value as an HTML-encoded plain text.
public string asText ( $value ) | ||
$value | string | The value to be formatted. |
return | string | The formatted result. |
---|
Formats the value as a time.
See also $timeFormat.
public string asTime ( $value, $format = null ) | ||
$value | integer|string|DateTime | The value to be formatted. The following types of value are supported:
The formatter will convert date values according to $timeZone before formatting it. If no timezone conversion should be performed, you need to set $defaultTimeZone and $timeZone to the same value. |
$format | string | The format used to convert the value into a date string. If null, $timeFormat will be used. 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 |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value can not be evaluated as a date value. |
throws | yii\base\InvalidConfigException | if the date format is invalid. |
Formats a date, time or datetime in a float number as UNIX timestamp (seconds since 01-01-1970).
public string asTimestamp ( $value ) | ||
$value | integer|string|DateTime | The value to be formatted. The following types of value are supported:
|
return | string | The formatted result. |
---|
Formats the value as a hyperlink.
public string asUrl ( $value, $options = [] ) | ||
$value | mixed | The value to be formatted. |
$options | array | The tag options in terms of name-value pairs. See yii\helpers\Html::a(). |
return | string | The formatted result. |
---|
Formats the value as a weight in human readable form for example 12 kilograms
.
Check properties $baseUnits if you need to change unit of value as the multiplier of the smallest unit and $systemOfUnits to switch between UNIT_SYSTEM_METRIC or UNIT_SYSTEM_IMPERIAL.
public string asWeight ( $value, $decimals = null, $options = [], $textOptions = [] ) | ||
$value | float|integer | Value to be formatted. |
$decimals | integer | The number of digits after the decimal point. |
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | string | The formatted result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric or the formatting failed. |
throws | yii\base\InvalidConfigException | when INTL is not installed or does not contain required information. |
Creates a number formatter based on the given type and format.
You may override this method to create a number formatter based on patterns.
protected NumberFormatter createNumberFormatter ( $style, $decimals = null, $options = [], $textOptions = [] ) | ||
$style | integer | The type of the number formatter. Values: NumberFormatter::DECIMAL, ::CURRENCY, ::PERCENT, ::SCIENTIFIC, ::SPELLOUT, ::ORDINAL ::DURATION, ::PATTERN_RULEBASED, ::DEFAULT_STYLE, ::IGNORE |
$decimals | integer | The number of digits after the decimal point. |
$options | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterOptions. |
$textOptions | array | Optional configuration for the number formatter. This parameter will be merged with $numberFormatterTextOptions. |
return | NumberFormatter | The created formatter instance |
---|
Formats the value based on the given format type.
This method will call one of the "as" methods available in this class to do the formatting. For type "xyz", the method "asXyz" will be used. For example, if the format is "html", then asHtml() will be used. Format names are case insensitive.
public string format ( $value, $format ) | ||
$value | mixed | The value to be formatted. |
$format | string|array|Closure | The format of the value, e.g., "html", "text" or an anonymous function returning the formatted value. To specify additional parameters of the formatting method, you may use an array.
The first element of the array specifies the format name, while the rest of the elements will be used as the
parameters to the formatting method. For example, a format of The anonymous function signature should be: |
return | string | The formatting result. |
---|---|---|
throws | yii\base\InvalidArgumentException | if the format type is not supported by this class. |
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( ) |
Checks if string representations of given value and its normalized version are different.
protected boolean isNormalizedValueMispresented ( $value, $normalizedValue ) | ||
$value | string|float|integer | |
$normalizedValue | float|integer |
Normalizes the given datetime value as a DateTime object that can be taken by various date/time formatting methods.
protected DateTime|array normalizeDatetimeValue ( $value, $checkDateTimeInfo = false ) | ||
$value | integer|string|DateTime | The datetime value to be normalized. The following types of value are supported:
|
$checkDateTimeInfo | boolean | Whether to also check if the date/time value has some time and date information attached.
Defaults to |
return | DateTime|array | The normalized datetime value.
Since version 2.0.1 this may also return an array if |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value can not be evaluated as a date value. |
Normalizes a numeric string value.
protected string normalizeNumericStringValue ( $value ) | ||
$value | string | |
return | string | The normalized number value as a string |
---|
Normalizes a numeric input value.
protected float|integer normalizeNumericValue ( $value ) | ||
$value | mixed | The input value |
return | float|integer | The normalized number value |
---|---|---|
throws | yii\base\InvalidArgumentException | if the input value is not numeric. |
Signup or Login in order to comment.