Yii Tutorial on Yii Formatting

to display data in a readable format, you can use the formatter application component.

step1 − add the actionformatter method to the sitecontroller.

public function actionformatter(){
   return $this->render('formatter');
}

in the above code, we just render the formatter view.

step 2 − now, create a formatter.php view file inside the views/site folder.

<?php
   $formatter = \yii::$app->formatter;
   // output: january 1, 2016
   echo $formatter->asdate('2016-01-01', 'long'),"<br>";
   // output: 51.50%
   echo $formatter->aspercent(0.515, 2),"<br>";
   // output: <a href = "mailto:test@test.com">test@test.com</a>
   echo $formatter->asemail('test@test.com'),"<br>";
   // output: yes
   echo $formatter->asboolean(true),"<br>";
   // output: (not set)
   echo $formatter->asdate(null),"<br>";
?>

step 3 − go to http://localhost:8080/index.php?r=site/formatter, you will see the following output.

view file

the formatter component supports the following formats related with date and time −

output format example
date january 01, 2016
time 16:06
datetime january 01, 2016 16:06
timestamp 1512609983
relativetime 1 hour ago
duration 5 minutes

step 4 − modify the formatter view this way.

<?php
   $formatter = \yii::$app->formatter;
   echo $formatter->asdate(date('y-m-d'), 'long'),"<br>";
   echo $formatter->astime(date("y-m-d")),"<br>";
   echo $formatter->asdatetime(date("y-m-d")),"<br>";

   echo $formatter->astimestamp(date("y-m-d")),"<br>";
   echo $formatter->asrelativetime(date("y-m-d")),"<br>";
?>

step 5 − type http://localhost:8080/index.php?r=site/formatter in the address bar of your web browser, you will see the following output.

formatter output

date formats

there are also four date format shortcuts: short, medium, long, and full.

step 1 − modify the formatter view file this way.

<?php
   $formatter = \yii::$app->formatter;
   echo $formatter->asdate(date('y-m-d'), 'short'),"<br>";
   echo $formatter->asdate(date('y-m-d'), 'medium'),"<br>";
   echo $formatter->asdate(date('y-m-d'), 'long'),"<br>";
   echo $formatter->asdate(date('y-m-d'), 'full'),"<br>";
?>

step 2 − if you go to the web browser and type http://localhost:8080/index.php?r=site/formatter, you will see the following output.

data formats output

number formats

the formatter component supports the following formats related with numbers −

output format example
integer 51
decimal 105.51
percent 51%
scientific 1.050000e+2
currency $105
size 105 bytes
shortsize 105 b

step 1 − modify the formatter view this way.

<?php
   $formatter = \yii::$app->formatter;
   echo yii::$app->formatter->asinteger(105),"<br>";
   echo yii::$app->formatter->asdecimal(105.41),"<br>";
   echo yii::$app->formatter->aspercent(0.51),"<br>";
   echo yii::$app->formatter->asscientific(105),"<br>";
   echo yii::$app->formatter->ascurrency(105, "$"),"<br>";
   echo yii::$app->formatter->assize(105),"<br>";
   echo yii::$app->formatter->asshortsize(105),"<br>";
?>

step 2 − go to http://localhost:8080/index.php?r=site/formatter, you will see the following output.

number formats output

other formats

yii also supports other formats −

  • text − the value is html-encoded.

  • raw − the value is outputted as is.

  • paragraphs − the value is formatted as html text paragraphs wrapped into the p tag.

  • ntext − the value is formatted as an html plain text where newlines are converted into line breaks.

  • html − the value is purified using htmlpurifier to avoid xss attacks.

  • image − the value is formatted as an image tag.

  • boolean − the value is formatted as a boolean.

  • url − the value is formatted as a link.

  • email − the value is formatted as a mailto-link.

the formatter may use the currently active locale to determine how to format a value for a specific country.

the following example shows how to format date for different locales.

<?php
   yii::$app->formatter->locale = 'ru-ru';
   echo yii::$app->formatter->asdate('2016-01-01'); // output: 1 января 2016 г.
   yii::$app->formatter->locale = 'de-de';
   // output: 1. januar 2016
   echo yii::$app->formatter->asdate('2016-01-01');
   yii::$app->formatter->locale = 'en-us';
   // output: january 1, 2016
   echo yii::$app->formatter->asdate('2016-01-01');
?>