This article mainly introduces the Yii2 GridView date formatting and implementation date searchable tutorial related materials, the need for friends can refer to the following
First show you the date format, if you are satisfied, please continue to read:
We'll discuss the situation.
1, if your database field Created_at time format is date or datetime, it is very simple, the GridView directly output the field Created_at, as shown in the right
2. If the timestamp type of the database is stored, as shown in the left side, you need to output 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 above shows two ways to format the output, all can. However, if you want to implement the search mechanism, if your database is stored in a datetime type, it is convenient, dataprovider do not have to make changes,
The code is as follows
$query->andfilterwhere ([//...] ' created_at ' = $this->created_at,//...]);
If your database is stored in a timestamp.
The first step is to modify the corresponding rule as shown in
The second step, modify the Dataprovider can refer to the following code
The format that we enter in the search input box is generally 2016-01-01 instead of timestamp//output 2016-01-01 is the data that you want to search for this day, so 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} ");}
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!