Home >Backend Development >PHP Tutorial >yii2 GridView date formatting and date searchability case android gridview usage .net gridview c gridview
Author: Bailang Source: http://www.manks.top/article/yii2_gridview_dateformat_search
The copyright of this article belongs to the author. Reprinting is welcome, but this statement must be retained without the author's consent, and the original text must be given in a prominent position on the article page connection, otherwise we reserve the right to pursue legal liability.
Date formatting, let’s take a look at the renderings first
We will discuss this on a case-by-case basis
1. If the time format stored in your database field created_at is date or datetime, it is very simple. It can be output directly in the gridview The field created_at is enough, as shown on the right side of the picture above
2. If the timestamp type stored in the database is as shown on the left side of the picture above, it needs to be 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 methods You can do any format output. However, if you want to implement a search mechanism, if your database stores a datetime type, it is very convenient. The dataProvider does not need to be modified.
The code is as follows
$query->andFilterWhere([ // ...... 'created_at' => $this->created_at, // ...... ]);
If your database stores a timestamp,
No. First step, modify the corresponding rules as shown in the figure below
Second step, modify the dataProvider, you can refer to the following code
//我们搜索输入框中输入的格式一般是 2016-01-01 而非时间戳 //输出2016-01-01无非是想搜索这一天的数据,因此代码如下 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, personally think it is very troublesome to save timestamps, if you have a good If you have any suggestions, leave a message below and we can discuss them together.
The above introduces the case of yii2 GridView date formatting and making the date searchable, including the GRIDVIEW content. I hope it will be helpful to friends who are interested in PHP tutorials.