php 在yii2中的两个日期之间搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30325734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:42:40  来源:igfitidea点击:

search between two dates in yii2

phpyii2

提问by Yang

A date can be represented in different formats. A table itself looks like so:

日期可以用不同的格式表示。表本身看起来像这样:

   book varchar(250) NOT NULL,  
   date INT NOT NULL

Now my problem is that I can not implement search in range between two dates. For example, there are 5 books with different dates, but the start date starts at 31/12/14and final date is 31/02/15. So when a user selects a range between these dates, it must provide all books in that date range.

现在我的问题是我无法在两个日期之间的范围内实现搜索。例如,有 5 本书的日期不同,但开始日期为31/12/14,最终日期为31/02/15。因此,当用户选择这些日期之间的范围时,它必须提供该日期范围内的所有图书。

Is there any way to do it in Yii2? I could not find anything so far

在 Yii2 中有什么方法可以做到吗?到目前为止我找不到任何东西

UPDATE

更新

I'm implementing a custom filter which doesn't belong to GridViewand it looks like as a standalone box outside the table.

我正在实现一个不属于GridView它的自定义过滤器,它看起来像是表外的一个独立框。

It looks like so:

它看起来像这样:

<div class="custom-filter">

   Date range:
     <input name="start" />
     <input name="end" />

   Book name:
     <input name="book" />

</div>

回答by Leye Odumuyiwa

I believe this is the answer you need:

我相信这是您需要的答案:

$model = ModelName::find()
->where(['between', 'date', "2014-12-31", "2015-02-31" ])->all();

回答by pa3py6aka

If you get start and end in date format, but date in your database table is in INT type, you must do something like this:

如果您以日期格式获取开始和结束日期,但数据库表中的日期是 INT 类型,则必须执行以下操作:

//Get values and format them in unix timestamp
$start = Yii::$app->formatter->asTimestamp(Yii::$app->request->post('start'));
$end = Yii::$app->formatter->asTimestamp(Yii::$app->request->post('end'));

//Book name from your example form
$bookName = Yii::$app->request->post('book');

//Then you can find in base:
$books = Book::find()
    ->where(['between', 'date', $start, $end])
    ->andWhere(['like', 'book', $bookName])
    ->all();

Not forget validate values given from post.

不要忘记验证帖子给出的值。

回答by Khotim

Assuming the date stored as integer represents unix timestamp, you can make a model class and apply yii\validators\DateValidatoron startand endattributes.

假设存储为整数的日期代表 unix 时间戳,您可以创建一个模型类并将yii\validators\DateValidator应用于startend属性。

/**
 * Class which holds all kind of searchs on Book model.
 */
class BookSearch extends Book
{
    // Custom properties to hold data from input fields
    public $start;
    public $end;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['start', 'date', 'timestampAttribute' => 'start', 'format' => 'php:d/m/y'],
            ['end', 'date', 'timestampAttribute' => 'end', 'format' => 'php:d/m/y']
        ];
    }

    public function searchByDateRange($params)
    {
        $this->load($params);

        // When validation pass, $start and $end attributes will have their values converted to unix timestamp.
        if (!$this->validate()) {
            return false;
        }

        $query = Book::find()->andFilterWhere(['between', 'date', $this->start, $this->end]);

        return true;
    }
}

See more about timestampAttributeon this documentation.

查看更多有关timestampAttribute本文档

回答by kamlesh.bar

use Yii2 Active Recordand access books between two dates like this.

使用Yii2 Active Record并像这样在两个日期之间访问书籍。

public static function getBookBetweenDates($lower, $upper)
{
    return Book::find()
        ->where(['and', "date>=$lower", "date<=$upper"])
        ->all();
}

I assume you are using active record class and you have created Book.php (appropriate name based on table name) As model file .

我假设您正在使用活动记录类并且您已经创建了 Book.php(基于表名的适当名称)作为模型文件。