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/14
and 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.
Is there any way to do it in Yii2? I could not find anything so far
UPDATE
I'm implementing a custom filter which doesn't belong to GridView
and it looks like as a standalone box outside the table.
It looks like so:
<div class="custom-filter">
Date range:
<input name="start" />
<input name="end" />
Book name:
<input name="book" />
</div>
I believe this is the answer you need:
$model = ModelName::find()
->where(['between', 'date', "2014-12-31", "2015-02-31" ])->all();
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:
//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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With