Home > Backend Development > PHP Tutorial > yii2 GridView date formatting and date searchability case android gridview usage .net gridview c gridview

yii2 GridView date formatting and date searchability case android gridview usage .net gridview c gridview

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 08:52:26
Original
990 people have browsed it

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

android gridview,gridview属性,c# gridview,gridview android,asp.net gridview,gridview 分页,android gridview用法,.net gridview,c gridview

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'],
],
Copy after login

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,
    // ......
]);
Copy after login

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}");
}
Copy after login

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.

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template