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.
PHP* Yii2

Avatar of undefined
Last Comment
Daniel Pineault

8/22/2022 - Mon
lenamtl

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

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

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Your help has saved me hundreds of hours of internet surfing.
fblack61