Wilmer Arambula
Wilmer Arambula

Reputation: 31

Format Time Widget ActiveForm yii2

Good afternoon I need to format the input of a time type field in the widget active form, my form should be short time without the seconds, the field is defined in the table of type time, is there any way to tell it to the widget actiform or model this field is of format "php: h: i".

Thank you very much,

Wilmer.

<?= $form->field($model, 'turnos008')->textInput(['class' => 'form-control col-md-2 inicio']) ?>

Upvotes: 0

Views: 2642

Answers (2)

user206
user206

Reputation: 1105

Display In form: format "php: h: i"

<?= $form->field($model,'fieldTime')->textInput(['value' => date('h:i', $model->fieldTime)]) ?>

<?php if (extension_loaded('intl')): ?>
   <?= $form->field($model,'fieldTime')->textInput(['value' => \Yii::t('user', '{0, date, hh:mm}', [$model->fieldTime])]) ?>
<?php endif ?>

<?= $form->field($model,'fieldTime',['inputOptions' => ['value' => \Yii::$app->formatter->asTime($model->fieldTime, 'php:h:i')]]) ?>

<?= $form->field($model,'fieldTime')->textInput(['value' => \Yii::$app->formatter->asDate($model->fieldTime, 'php:h:i')]) ?>

Note: Change 'fieldTime' as your field name

Rull validation: Link

[
    [['from_date', 'to_date'], 'date'],
    [['from_datetime', 'to_datetime'], 'datetime'],
    [['some_time'], 'time'],
]

Upvotes: 0

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

Before rendering your field(In Controller for example), you can do like below:

I assumed that the time is 12:20:21 AM

//for example: output is : 12:20 AM
$model->turnos008=Yii::$app->formatter->asTime($model->turnos008, 'short');

//for example: output is : 12:20:21 AM
$model->turnos008=Yii::$app->formatter->asTime($model->turnos008, 'medium');

//for example: output is : 12:20:21 AM GMT
$model->turnos008=Yii::$app->formatter->asTime($model->turnos008, 'long');

//for example: output is : 12:20
$model->turnos008=Yii::$app->formatter->asTime($model->turnos008, 'php:h:i');

You may take a look at Yii2's official document on http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asTime()-detail

Upvotes: 0

Related Questions