Analysis of Yii2GridView date formatting and implementation of date searchable tutorial _ php instance

Source: Internet
Author: User
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!

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.