Link to home
Start Free TrialLog in
Avatar of Daniel Pineault
Daniel Pineault

asked on

Apply datetime formatter to form field

Using Yii2, I'm trying to apply a format to a Datetime field/control

echo $form->field(
                                    $modelLeg, 
                                    "[{$i}]StartDt",
                                    [
                                        'options' => ['class' => 'table-control'],
                                        'template' => '<div class="">{input}</div>'."\n".'<div class="">{hint}{error}</div>'
                                    ]
                                )
                                ->widget(
                                    DateTimePicker::className(), [
                                        'type' => DateTimePicker::TYPE_INPUT, 
                                        'pluginOptions' => [
                                            'autoclose'=>true,
                                            'format' => 'yyyy-mm-dd hh:ii',
                                            'todayHighlight' => true,
                                        ]
                                    ]
                                );

Open in new window


I set the formatter in my config file, but it doesn't seem to do anything?!
'formatter' => [
            'class' => 'yii\i18n\Formatter',
            'dateFormat' => 'yyyy-MM-dd', //'php:Y-m-d',
            'timeFormat'=>'HH:mm', //php:H:i',
            'datetimeFormat'=>'yyyy-MM-dd HH:mm', //'php:Y-m-d H:i',
            'currencyCode' => '$',
            'decimalSeparator' => '.',
            'thousandSeparator' => ',',
        ],

Open in new window



I believe this is basically what I need to do
echo $form->field(
                                    $modelLeg, 
                                    "[{$i}]StartDt",
                                    [
                                        'options' => ['class' => 'table-control'],
                                        // 'inputOptions' => ['enableLabel' => false,]
                                        // 'inputOptions' => ['value' => \Yii::$app->formatter->asDatetime($modelLeg->StartDt, 'M/d/Y')],
                                        'template' => '<div class="">{input}</div>'."\n".'<div class="">{hint}{error}</div>',
                                    ]
                                )

Open in new window

but it doesn't work and I think this has to do with the dynamic form attribute not being StartDt but rather [{$i}]StartDt.  However, I can't get it to accept any of the variations I've attempted.


No matter what I do, I always have seconds displayed and I only want hour and minutes

could someone either point out what I'm doing wrong with the config file or how I can specify the formatter to the $form->field().

Thank you for the helping hand.
Avatar of lenamtl
lenamtl
Flag of Canada image

Have you check errors from logs
and in browser right click inspect  / console for errors
Avatar of Daniel Pineault
Daniel Pineault

ASKER

There are no error or other issues being reported in the console or debugger.
ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial