$dateFormat property of class yii\i18n\Formatter does not work!

In the following code:

$config = [

‘components’ => [
‘formatter’ => [
‘dateFormat’ => ‘dd.MM.yyyy’,
‘nullDisplay’ => ‘(non impostato)’,
],

$dateFormat property of class yii\i18n\Formatter does not work, because date fields on a form are not displayed in the format ‘dd.MM.yyyy’ but in the normal format ‘yyyy-MM-dd’.

Instead the $nullDisplay property does its job showing the correct value set, i.e. showing ‘(Non impostato)’

use _form
use kartik\date\DatePicker;

                    echo $form->field($model, 'sorteossorteo')->widget(DatePicker::classname(), [
                        'options' => ['placeholder' => 'Fecha'],
                        'pluginOptions' => [        
                            'format' => 'yyyy/mm/dd',
                             'autoclose'=>true,
                             'size' => 'lg',
                            'todayHighlight' => true,
                            'todayBtn' => true,
                        ]
                    ])->label(false);         
                     ?>

install in:
composer.json add
“kartik-v/yii2-widget-datepicker”: “@dev”,

update composer

post the form

thanks but i had already tried with kartik \date\DatePicker …

This is the code line in the form:

<?php echo $form->field($model, 'dataSentenza')->widget(yii\jui\DatePicker::classname(), [ 'language' => 'it', 'dateFormat' => 'yyyy-MM-dd', ]) ?>

I solved it by acting on the data column of the gridview widget:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],


[
‘attribute’ => ‘dataSentenza’,
‘format’ => [‘date’, ‘php:d-m-Y’]
],

I will have to do the same thing in all views where the date is displayed.
However I still do not understand why acting on the Formatter does not work …

This have nothing to do with Yii formatter, just FYI

this one depends on yii formatter.

[
    'attribute' => 'dataSentenza',
   'value' => function($model){
        return  Yii::$app->formatter->asDate($model->dataSentenza, 'php:d.m.Y');
    }
],

No need to change formatter configuration

1 Like

I recommend that you use the y-m-d format, it is easy for you to filter between date, and the database table format date ‘y-m-d’

don’t use, ‘php: d.m.Y’
with the configuration of the table it works for you

Table
image

image

… ok, in fact I would like to leave the y-m-d format in the table but in the DatePicker only show the d-m-y format (I hope I was clear, sorry but I’m not very familiar with yii)

image

Excuse me, I do not understand you, you enter the code as I sent it to you, it is shown in this way

Ok, but if I do this then the date in this format does not pass the validator check and I get the message “The format of Data Sentenza is invalid” … because the validator expects the date in yyyy-MM-dd format … (I know I’m doing something wrong …)
If, on the other hand, I set both the format of the validator and that of the DatePicker to dd-MM-yyyy I have no problems neither in displaying the form nor in validation but … then in the DB I end up with the value 0000-00-00! which is obviously not acceptable …

TBH, I do not understand what you are actually asking.

You’re right, I’ve made a bit of confusion, I realized, thanks to you, that the Formatter has nothing to do with it.
So I try to summarize my problem more clearly:
In a Date field on a form I would like to display the date in dd-MM-yyyy format

<?php echo $form->field($model, 'dataSentenza')->widget(yii\jui\DatePicker::classname(), [ 'language' => 'it-IT', 'dateFormat' => 'dd-MM-yyyy' ]) ?>

Questo la funzione rules:

public function rules()
{   return [                        
        [['dataSentenza', 'dataDeposito'], 'date', 'format' => 'dd-MM-yyyy']

The problem is if I do that, whatever value I enter in the field, in the DB end up :
immagine

for this reason, to make sure that the correct value of the inserted date ends up in the DB, I changed the code as follows:

<?php echo $form->field($model, 'dataSentenza')->widget(yii\jui\DatePicker::classname(), [ 'language' => 'it-IT', 'dateFormat' => 'yyyy-MM-dd' ]) ?>
public function rules()
{   return [                        
        [['dataSentenza', 'dataDeposito'], 'date', 'format' => 'yyyy-MM-dd']

Now the inserted date is written correctly in the DB, but I can’t visualize the date in the format I wanted (dd-MM-yyyy)…

I hope I was clearer .
Anyway thanks for your patience …

Hi,
That sounds to me like the database is rejecting wrong format replacing it with default 0000-00-00

So if it is really important to you, you need to reformat your model code to format that database understands in beforeSave() method of your Model and then reconvert for display in afterFind() method of the same

Thanks, I will try it…