troubleshooting Question

Apply datetime formatter to form field

Avatar of Daniel Pineault
Daniel Pineault asked on
PHP* Yii2
3 Comments1 Solution71 ViewsLast Modified:
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,
                                        ]
                                    ]
                                );

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' => ',',
        ],


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>',
                                    ]
                                )
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.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros