What are formatter in Yii2 ?

devquora
devquora

Posted On: Feb 22, 2018

 

Formatter are Yii application component that is used format view data in readable format for users. By default the formatter is implemented by yii\i18n\Formatter which provides a set of methods to format data as date/time, numbers, currencies,and other commonly used formats. You can use the formatter like the following,

$formatter = \Yii::$app->formatter;

// output: January 1, 2014
echo $formatter->asDate('2014-01-01', 'long');
 
// output: 12.50%
echo $formatter->asPercent(0.125, 2);
 
// output: cebe@example.com
echo $formatter->asEmail('cebe@example.com'); 

// output: Yes
echo $formatter->asBoolean(true); 
// it also handles display of null values:

// output: (not set)
echo $formatter->asDate(null); 

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Yii 2 Interview Questions

    Which PHP version is required to install Yii 2.0 ?

    Which PHP version is required to install Yii 0 ?..

    Yii 2 Interview Questions

    What is latest version Yii 2 Framework ?

    The latest version of Yii 2 is 2.0.15, released on March 20, 2018...