Yii2如何根据系统语言配置自动获取日期格式化掩码,解决DatePicker日期存储转换错误问题
Here's my problem: I have a date field in a Yii2 form that I am formatting as showed in the following snippet:
<?= $form->field($model, 'date_last_purchase')->widget(DatePicker::class, [ 'language' => Yii::$app->language, // Set language based on application configuration 'pluginOptions' => [ 'convertFormat' => true, ], ]) ?>
I am using the use kartik\date\DatePicker component for that.
My language configuration is set to pt-BR, and it is correctly displaying the date in pt-BR format in the form view.
However, it is not converting properly to save it in the database. The format of a pt-BR date is d/m/Y. When autoconverting and saving it to the database, Yii2 is saving it as if it were m/d/Y. For instance, if the date specified on the form view is 'November 5, 2024' (05/11/2024 in pt-BR), it is storing it as 2024-05-11 (May 11, 2024).
I could disable the convertFormat option and convert it manually, but I would like to know if there is a way to make the formatter return the mask being used on the view dynamically (according to the language settings) so I can use it to make the conversion.
Or, better yet, if there is a way to tell the DatePicker widget what format it should use as input to make the conversion correctly.
Thanks in advance.
备注:内容来源于stack exchange,提问作者Victor Sebben

