Home  >  Article  >  Backend Development  >  Formatted output data of yii framework

Formatted output data of yii framework

小云云
小云云Original
2018-03-21 10:52:311734browse


This article mainly shares with you the formatted output data of the yii framework, hoping to help everyone.

Format time/date data

Several formats are supported by default

  • <p>date: This variable will be formatted as date January 01, 2014.

  • <p>time: This variable will be formatted as time 14:23.

  • <p>datetime: This variable will be formatted as date+time January 01, 2014 14:23.

  • <p>timestamp: This variable will be formatted as UNIX timestamp unix timestamp, for example 1412609982.

  • <p>relativeTime: This variable will be formatted as the human readable current relative time 1 hour ago.

  • <p>duration: This variable will be formatted as a human-readable duration 1 day, 2 minutes.

Time/date data is formatted by default using date, time, datetime methods. You can configure some of them by yourself, just configure dateFormat, timeFormat, and datetimeFormat in the configuration file.

At the same time, you can also configure it to use ICU syntax, and you can also configure it to use PHP date() syntax, just add the php: prefix.

Format numbers

formatter Supports the following methods

  • <p>integer: This variable will be formatted as an integer e.g. 42 .

  • <p>decimal: This variable will be formatted as a float with the specified precision with a comma e.g. 2,542.123 or 2.542,123 .

  • <p>percent: This variable will be formatted as a percentage e.g. 42%.

  • <p>scientific: This variable will be formatted as scientific notation e.g. 4.2E4.

  • ##currency: This variable will be formatted as currency <p>?420.00 . Please make sure you have configured it correctly before using this method. locale

  • size: This variable will be formatted as a human-readable number of bytes, e.g. <p>410 kibibytes.

  • shortSize: This variable will be formatted as a human-readable number of bytes (abbreviation) size, e.g. <p>410 KiB.

You can use decimalSeparator and thousandsandSeparator to make adjustments. They will all be formatted according to the current locale.

If you want more advanced configuration, you can use yii\i18n\Formatter::$numberFormatterOptions and yii\i18n\Formatter::$numberFormatterTextOptions, NumberFormatter class to format.

For example, in order to adjust the maximum and minimum values ​​of the decimal part, you can configure yii\i18n\Formatter::$numberFormatterOptions as follows:

'numberFormatterOptions' => [ NumberFormatter::MIN_FRACTION_DIGITS => 0, NumberFormatter::MAX_FRACTION_DIGITS => 2,]

Other formatting

In addition to time/date and number formatting, Yii also supports the following common formatting

  • <p>raw: Except that null will be formatted by nullDisplay, output it as is.

  • <p>text: Encoded into HTML format. At the same time, this is also the method used by GridView DataColumn by default.

  • <p>ntext: Encoded to HTML format, line breaks will also be converted.

  • <p>paragraphs: Encoded in HTML format, wrapped in <p> tags.

  • <p>html: This value will be filtered by HtmlPurifier to prevent XSS attacks. You can add some configurations such as ['html', ['Attr.AllowedFrameTargets' =&gt ; ['_blank']]].

  • <p>email: This value will be converted into a mailto link.

  • <p>

  • <p>

  • <p>boolean: true => Yes, false => No, you can do another Configuration: yii\i18n\Formatter::$booleanFormat.

Null value

Null value (null) will be specially formatted. fommater The default format will be null value into the current language corresponding to (not set). You can configure the nullDisplay attribute for personalization.

Local date formatting

formatter The current locale will be used to determine the formatted content. For the same date, different time zone configurations will have different output

The above is the detailed content of Formatted output data of yii framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn