<p>I got the answer to this thanks to @BrandonKelly from a comment in this question: <a href="https://craftcms.stackexchange.com/questions/1680/how-can-you-return-elements-between-two-values/1683?noredirect=1#comment30865_1683">How can you return elements between two values?</a></p>

<p>Turning it into a subarray does the trick: <code>expirationDate: [ 'and', '&gt;=' ~ now, '&lt;=' ~ now|date_modify('+2 weeks') ]</code></p>

<p>Thanks, @BrandonKelly!</p>

CC BY-SA 4.0
2020-11-18T16:15:21.680Z
This is no longer accurate in Craft v3. The dates in the query need to be explicitly formatted as strings. For example `expirationDate: [ 'and', '>=' ~ now|date('U'), '<=' ~ now|date_modify('+2 weeks')|date('U') ]` See https://craftcms.stackexchange.com/a/25826
Jameal G
CC BY-SA 3.0
2017-10-26T21:11:01.907Z
2017-10-26T21:11:01.907Z
Steven Thate
<p>I am trying to query entries (scripts) when a custom expiration date field is within a certain range.</p>

<p>These questions/answers helped me get as far as I am, but I'm still stuck: <a href="https://craftcms.stackexchange.com/questions/11014/advanced-query-filter-based-on-date-and-values">Advanced query/filter based on date and values</a> and <a href="https://craftcms.stackexchange.com/questions/1680/how-can-you-return-elements-between-two-values">How can you return elements between two values?</a></p>

<p>Here's the full scenario. On this app's homepage, it lists a handful of recent scripts (prescriptions). It also has sections where it displays scripts that are expired and scripts that are about to expire (within two weeks of the current date).</p>

<p>I did some testing and found that the following conditionals filter the scripts just how I want:</p>

<pre><code>{# When expiration date is between now and 2 weeks #}
{% if expirationDate &gt;= now and expDate &lt;= now|date_modify('+2 weeks') %}

{# When expiration date is anytime before now #}
{% if expirationDate &lt; now %}
</code></pre>

<p>Those work great. <strong>How can I move this filter into the query itself?</strong></p>

<p>I was thinking something like the following, but it obviously doesn't work because of all the commas. But this is what I'm trying to do. <strong>How do I use this correctly in a hash?</strong></p>

<pre><code>{% set scriptsAboutToExpire = {
    section: 'scripts',
    order: 'expirationDate asc',
    expirationDate: 'and', '&gt;=' ~ now, '&lt;=' ~ now|date_modify('+2 weeks')
} %}
</code></pre>

<entries><datetime><query>
Query entries between two values
# Query entries between two values

(Asked by: username_0 on 2017-10-26)

I am trying to query entries (scripts) when a custom expiration date field is within a certain range.
These questions/answers helped me get as far as I am, but I'm still stuck: Advanced query/filter based on date and values and How can you return elements between two values?
Here's the full scenario. On this app's homepage, it lists a handful of recent scripts (prescriptions). It also has sections where it displays scripts that are expired and scripts that are about to expire (within two weeks of the current date).
I did some testing and found that the following conditionals filter the scripts just how I want:
{# When expiration date is between now and 2 weeks #}
{% if expirationDate >= now and expDate <= now|date_modify('+2 weeks') %}

{# When expiration date is anytime before now #}
{% if expirationDate < now %}

Those work great. How can I move this filter into the query itself?
I was thinking something like the following, but it obviously doesn't work because of all the commas. But this is what I'm trying to do. How do I use this correctly in a hash?
{% set scriptsAboutToExpire = {
    section: 'scripts',
    order: 'expirationDate asc',
    expirationDate: 'and', '>=' ~ now, '<=' ~ now|date_modify('+2 weeks')
} %}

**Tags:** entries, datetime, query

---

## 1 answers

### Answer by username_0 on 2017-10-26. Score: 6 | Accepted answer

I got the answer to this thanks to @BrandonKelly from a comment in this question: [How can you return elements between two values?](https://craftcms.stackexchange.com/questions/1680/how-can-you-return-elements-between-two-values/1683?noredirect=1#comment30865_1683)

Turning it into a subarray does the trick: `expirationDate: [ 'and', '>=' ~ now, '<=' ~ now|date_modify('+2 weeks') ]`

Thanks, @BrandonKelly!

> **Comments:**
> **username_1**: This is no longer accurate in Craft v3. The dates in the query need to be explicitly formatted as strings. For example `expirationDate: [ 'and', '>=' ~ now|date('U'), '<=' ~ now|date_modify('+2 weeks')|date('U') ]` See https://craftcms.stackexchange.com/a/25826  

---

