Analysis of Yii2GridView date formatting and implementation of date searchable tutorials, yii2gridview_PHP tutorials

Source: Internet
Author: User
Analysis of Yii2GridView date formatting and implementation of date searchable tutorial, yii2gridview. Analysis of the Yii2GridView date formatting and implementation of the date searchable tutorial, yii2gridview first shows you the date formatting. if you are satisfied, please continue reading: in this article, we will analyze the Yii2 GridView date formatting and implement the date searchable tutorial, yii2gridview

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. Thank you very much for your support for the help House website!

GridView date formatting and implementation of the date searchable tutorial. yii2gridview will show you the date formatting first. if you are satisfied, please continue to read: This is our...

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.