Format date, time, and numbers
Now, let's see how to format the date, time, and numeric fields. Yii2 provides helpers for each of these types.
To format a value, we will use Yii::$app->formatter
; this object belongs to the Formatter
class located under yii\i18n\
and supports many types of formatting. All the methods used for this purpose start with an as
prefix. Therefore, the asDate
method will be used to format dates, and the asCurrency
method will be used to format currencies.
The first parameter of each formatting method is the value to be formatted and other fields refer to the format to be used and other optional parameters.
Let's change the view content by adding content of the Model that is ready to be saved:
<?php if($modelCanSave) { ?> <div class="alert alert-success"> Model ready to be saved! <br /><br /> These are values: <br /> Floor: <?php echo $model->floor; ?> <br /> Room Number: <?php...