This article mainly introduces the Yii2GridView date formatting and the implementation of date searchable tutorials. For more information, see
We will show you how to format the date. If you are satisfied, please continue reading:
We will discuss the situation in detail.
1. If the time format of your database Field created_at is date or datetime, it is very easy to directly output this field created_at in the gridview, as shown on the right
2. If the timestamp type stored in the database is shown on the left side of the data center, output the data as follows:
['attribute' => 'created_at','value' => function ($model) {return date('Y-m-d H:i:s', $model->created_at);},],['attribute' => 'created_at','format' => ['date', 'Y-m-d H:i:s'],],
The preceding two formats can be output. However, if you want to implement the search mechanism, if your database is saved in the datetime type, it is very convenient, dataProvider does not need to be modified,
The Code is as follows:
$query->andFilterWhere([// ......'created_at' => $this->created_at,// ......]);
If your database is saved with a timestamp.
Step 1: Modify the corresponding rule, as shown in
Step 2: Modify dataProvider by referring to the following code:
// In the search input box, the input format is generally, instead of the timestamp. // the output of 2016-01-01 is nothing more than the data of the day. Therefore, the Code is as follows if ($ this-> created_at) {$ createdAt = strtotime ($ this-> created_at); $ createdAtEnd = $ createdAt + 24*3600; $ query-> andWhere ("created_at >={$ createdAt} AND created_at <={ $ createdAtEnd }");}
Here is a small summary. It is recommended to use the datetime type. I personally think it is very troublesome to save the timestamp. If you have any good suggestions, please leave a message for me to learn and make progress together. I would like to thank you for your support for the script home website!