Yii2: date formatting for display

I set the definition_definition date field as datetime in mysql db.

In my model, the Appointment.php rules are set as follows:

 public function rules() { return [ [['appointment_date','weekdays'], 'safe'], [['appointment_date'], 'date','format' => 'dM-yyyy H:m'], 

and in web.php in the component I installed

 'formatter' => [ 'defaultTimeZone' => 'UTC', 'timeZone' => 'Asia/Kolkata', 

and, in my opinion, I am trying to format the date for display as follows:

 [ Yii::$app->formatter->asDatetime('appointment_date') ], 

Now I get the error message -

 appointment_date' is not a valid date time value: DateTime::__construct(): Failed to parse time string (appointment_date) at position 0 (a): The timezone could not be found in the database Array ( [warning_count] => 1 [warnings] => Array ( [6] => Double timezone specification ) [error_count] => 3 [errors] => Array ( [0] => The timezone could not be found in the database [11] => Unexpected character [12] => Double timezone specification ) 

While the date stored in the database is similar: 2015-01-20 11:50:00

If I do not format the date and just save the attribute as appointment_date , then the date is displayed as 2015-01-20 11:50:00

I want to show the date as 20-01-2015 11:50:00

If I use this code:

 [ 'attribute'=>'appointment_date', 'format'=>['DateTime','php:dmY H:i:s'] ], 

I formatted the date correctly.

I want to know what I'm doing wrong here when using

 Yii::$app->formatter->asDatetime('appointment_date') 

thanks

+5
source share
4 answers

I think this is just a little typo. You pass the string to the asDateTime method where it needs the value

 Yii::$app->formatter->asDatetime('appointment_date') 

it should be

 Yii::$app->formatter->asDatetime($model->appointment_date) 

Docs: http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asDatetime()-detail

+6
source

Ok it's that simple i need to use

 'appoinment_date:date' 

or

 'appointment_date:datetime' 

and add the format to the component builder

 'formatter' => [ 'defaultTimeZone' => 'UTC', 'timeZone' => 'Asia/Kolkata', 'dateFormat' => 'php:dm-Y', 'datetimeFormat'=>'php:dMY H:i:s' 

and now it works fine.

+6
source

My date type is date , so it stores and gives a response in the form of "YYYY-DD-MM" .
I did this in a look (index.php) without changing the logic or data of the database.

it was originally

 'date' 

which I changed with this

  [ 'attribute' => 'date', 'value' => function ($model, $key, $index, $widget) { return date("dmY", strtotime($model->date)); }, ], 

This works great for me,
Hope this will work for a little more needy.

0
source

Put the code below in the component section in Project / config / web.php

 'formatter' => [ 'defaultTimeZone' => 'UTC', 'timeZone' => 'Asia/Kolkata', 'dateFormat' => 'php:dm-Y', 'datetimeFormat'=>'php:dMY H:i:s' ] 

Where you display the date or time in your grid, just add this for the date

 'date_column_name:date' 

For date and time

 'datetime_column_name:datetime' 

This is it.It will work for the whole project where you want to change the date or date format.

0
source

Source: https://habr.com/ru/post/1211553/


All Articles