0

I want to select items which are in a range of two different dates (datetime type in Database). In this case want to select all items in range of dateStart and dateEnd.

Example table data:

id | dateStart  | dateEnd
---+------------+-----------+
3  | 2017-01-05 | 2017-01-07
6  | 2017-01-06 | 2017-01-10
8  | 2017-01-05 | 2017-01-11
11 | 2017-01-03 | 2017-01-04
12 | 2017-01-04 | 2017-01-10
15 | 2017-01-04 | 2017-01-18
17 | 2017-01-08 | 2017-01-10
25 | 2017-01-12 | 2017-01-15
31 | 2017-01-11 | 2017-01-24

Image for clarification:

enter image description here


Please note that I don't want to search between dates like:

Model::find()
    ->where(['between', 'dateStart', "2017-01-06", "2017-02-11"])
    ->all();

.. because between is not in range.

Using between results in this wrong output:

id | dateStart  | dateEnd
---+------------+-----------+
6  | 2017-01-06 | 2017-01-10
8  | 2017-01-05 | 2017-01-11
11 | 2017-01-03 | 2017-01-04
12 | 2017-01-04 | 2017-01-10
15 | 2017-01-04 | 2017-01-18

E.g. preferred ouput:

The selected (based on the table data above) items by searching startDate >= 2017-01-05and endDate <= 2017-01-11 should look like this:

id | dateStart  | dateEnd
---+------------+-----------+
3  | 2017-01-05 | 2017-01-07
6  | 2017-01-06 | 2017-01-10
8  | 2017-01-05 | 2017-01-11
12 | 2017-01-04 | 2017-01-10
15 | 2017-01-04 | 2017-01-18
17 | 2017-01-08 | 2017-01-10
31 | 2017-01-11 | 2017-01-24

Note: I'am looking for an answer based on Yii2 ActiveRecord models.

lin
  • 17,956
  • 4
  • 59
  • 83
  • could you explai the difference of one day for the dates in between sample and the dates in your searching sample ? .. i don't understand the difference you assign to between and the your search sample >= and <= is how between work.. fo what i now . – ScaisEdge Aug 22 '17 at 15:51
  • @scaisEdge Please take a look at the picture, it does explain it. I also added an ouput example when using `between`. – lin Aug 22 '17 at 15:56

1 Answers1

2

Using ActiveRecord you can do like this sequence of clauses ->orWhere:

$dateStart = '2017-01-05';
$dateEnd = '2017-01-11';

Model::find()
   ->orWhere(['between', 'dateEnd', $dateStart , $dateEnd]) 
   ->orWhere(['between', 'dateStart', $dateStart , $dateEnd]) 
   ->orWhere(['and', "dateEnd>='".$dateEnd."'", "dateStart<='".$dateStart ."'"])
   ->all();
lin
  • 17,956
  • 4
  • 59
  • 83
Oleg
  • 109
  • 4
  • Thanks but I'm looking for a yii2 active record model solution. I allready discovered this solution in the web. I'm not looking for an SQL solution. Thanks anyways. – lin Aug 22 '17 at 15:58
  • Ok. I understand. Do you need exactly Active Record? I'm afraid of AR doesn't have such opportunities. But with DAO (not plain SQL), you can use Query Builder. Using this http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#appending-conditions ; Query Builder is appropriate tool for complex queries. – Oleg Aug 22 '17 at 16:06
  • Oleg, yea I know but maybe there is a solution based on operators - http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where%28%29-detail so .. I think it is possible with AR. Maybe by using `<>` operator. – lin Aug 22 '17 at 16:08
  • ->orWhere(['between', 'dateEnd', $dateLeft, $dateRight]) ->orWhere(['between', 'dateStart', $dateLeft, $dateRight]) ->orWhere(['and', "dateEnd>=$dateRight", "dateStart<=$dateLeft"]); try like this if possible – Oleg Aug 22 '17 at 17:17
  • Great, this works like a charm. Could you update your answer? =) – lin Aug 22 '17 at 17:26