YII2 GridView Date formatted and implemented date searchable case Android GridView usage. NET GridView C GridView

Source: Internet
Author: User
Author: White Wolf Source: Http://www.manks.top/article/yii2_gridview_dateformat_search

The copyright belongs to the author, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Date format, let's start by looking at

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

Here is a small summary, it is recommended to use the datetime type, the personal feel that the time stamp is very troublesome, if you have good advice, below the message we share.

The above describes the YII2 GridView date format and the implementation of the date searchable case, including the GridView aspect of the content, I hope that the PHP tutorial interested friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.