<p>You could do two queries to get the Ids of the desired entries, <a href="http://twig.sensiolabs.org/doc/filters/merge.html" rel="nofollow">merge</a> the Ids and then query the entries with these Ids:</p>

<pre><code>{# Get Ids of desired entries #}
{% set startDateIds = craft.entries.startDate('and', '&gt;= ' ~ unixbegins, '&lt; ' ~ unixends).ids() %}
{% set endDateIds = craft.entries.endDate('and', '&gt;= ' ~ unixbegins, '&lt; ' ~ unixends).ids() %}

{# Merge queried Ids #}
{% set filteredIds = startDateIds|merge(endDateIds) %}

{# Get entries with Id paramenter #}
{% set filteredEntries = craft.entries.id(filteredIds) %}
</code></pre>

<p>.</p>

<p>Another idea is to query all potential entries, loop those and filter within the loop. This is probably the way to go if you need to combine your conditionals with <a href="http://twig.sensiolabs.org/doc/templates.html#logic" rel="nofollow">other logic</a> then the <code>and</code> operator:</p>

<pre><code>{# Get all potential entries #}
{% set allEntries = craft.entries.section('events').limit(null).find() %}

{# Define array before loop #}
{% set filteredEntries = {} %}

{# Loop all entries #}
{% for key, entry in allEntries %}

    {# Set conditionals to filter for desired entries #}
    {% if ( entry.startDate &gt;= unixbegins and entry.startDate &lt; unixends ) and ( entry.endDate &gt;= unixbegins and entry.endDate &lt; unixends ) %}

        {# Merge entries #}
        {% set filteredEntries = filteredEntries|merge({ (key): entry }) %}
    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-06-24T05:35:52.177Z
Error: The merge filter only works with arrays or hashes. Both entriesStartDate and entriesEndDate return a llist of entries correctly.
Clive Portman
CC BY-SA 3.0
2014-06-24T05:47:46.733Z
Actually, I don't want merge, do I? I only want entries which in both entriesStartDate and entriesEndDate.
Clive Portman
CC BY-SA 3.0
2014-06-24T08:42:06.173Z
Hmm, you wrote "with either a start date OR and end date within a calendar year" that means all entries with a start date in that year PLUS all entries with an end date in it.
carlcs
CC BY-SA 3.0
2014-06-24T10:27:54.863Z
Yeah, you're right. I do need merge. As long as they aren't repeated.
Clive Portman
CC BY-SA 3.0
2014-06-24T10:45:02.737Z
Yep, the new improved merge solved it. Thanks.
Clive Portman
CC BY-SA 3.0
2014-06-24T10:59:39.480Z
That's good to hear, @Clive. I'll delete that old code for clarity.
carlcs
CC BY-SA 3.0
2014-06-25T19:11:18.180Z
Just so you know, there's something in lines 2 and 3 of your first answer which knackers a Bootstrap column for Webkit browsers. I don't know quite where is was, but typing them out rather than pasting them fixed it.
Clive Portman
CC BY-SA 3.0
2014-06-23T16:35:05.410Z
2014-06-24T11:00:07.983Z
2014-06-24T11:00:07.983Z
carlcs
<p>I have two date fields in my entry and I want to return those entries with either a start date OR and end date within a calendar year.</p>

<p>I can get those with a startDate within the calendar year using:</p>

<pre><code>{% set nextyear = year +1 %}
{% set yearbegins = '1 January ' ~ year %}
{% set yearends = '1 January' ~ nextyear %}
{% set unixbegins = yearbegins | date("U") %}
{% set unixends = yearends | date("U") %}

{% set entries = craft.entries.section('events').limit(null).startDate('and', '&gt;= ' ~ unixbegins, '&lt; ' ~ unixends) %}
</code></pre>

<p>How to I then run a test on endDate to further filter them down?&nbsp;</p>

CC BY-SA 3.0
2014-06-23T16:38:44.843Z
How come this code works for you now? I think you had to use the `date('U')` filter inside the query as you wrote [here](http://craftcms.stackexchange.com/a/264/125)?
carlcs
CC BY-SA 3.0
2014-06-23T20:48:33.497Z
I don't know. I was surprised to find that happen in the other post, too. When I get chance I'll review it, but this is a different template.
Clive Portman
CC BY-SA 3.0
2014-08-06T11:49:20.803Z
thanks a lot. your code got me started for my own project. but where does this syntax come from (cf_date('and', '>= ' ~ unixbegins, '< ' ~ unixends, '< ' ~ now))? are there any docs on this?
outline4
<datetime><date><conditional><logic><queries>
Return entries with startDate or endDate within a calendar year
# Return entries with startDate or endDate within a calendar year

(Asked by: username_0 on 2014-06-23)

I have two date fields in my entry and I want to return those entries with either a start date OR and end date within a calendar year.
I can get those with a startDate within the calendar year using:
{% set nextyear = year +1 %}
{% set yearbegins = '1 January ' ~ year %}
{% set yearends = '1 January' ~ nextyear %}
{% set unixbegins = yearbegins | date("U") %}
{% set unixends = yearends | date("U") %}

{% set entries = craft.entries.section('events').limit(null).startDate('and', '>= ' ~ unixbegins, '< ' ~ unixends) %}

How to I then run a test on endDate to further filter them down?

**Tags:** datetime, date, conditional, logic, queries

> **Comments:**
> **username_1**: How come this code works for you now? I think you had to use the `date('U')` filter inside the query as you wrote [here](http://craftcms.stackexchange.com/a/264/125)?  
> **username_0**: I don't know. I was surprised to find that happen in the other post, too. When I get chance I'll review it, but this is a different template.  
> **username_2**: thanks a lot. your code got me started for my own project. but where does this syntax come from (cf_date('and', '>= ' ~ unixbegins, '< ' ~ unixends, '< ' ~ now))? are there any docs on this?  

---

## 1 answers

### Answer by username_1 on 2014-06-23. Score: 2 | Accepted answer

You could do two queries to get the Ids of the desired entries, merge the Ids and then query the entries with these Ids:
{# Get Ids of desired entries #}
{% set startDateIds = craft.entries.startDate('and', '>= ' ~ unixbegins, '< ' ~ unixends).ids() %}
{% set endDateIds = craft.entries.endDate('and', '>= ' ~ unixbegins, '< ' ~ unixends).ids() %}

{# Merge queried Ids #}
{% set filteredIds = startDateIds|merge(endDateIds) %}

{# Get entries with Id paramenter #}
{% set filteredEntries = craft.entries.id(filteredIds) %}

.
Another idea is to query all potential entries, loop those and filter within the loop. This is probably the way to go if you need to combine your conditionals with other logic then the and operator:
{# Get all potential entries #}
{% set allEntries = craft.entries.section('events').limit(null).find() %}

{# Define array before loop #}
{% set filteredEntries = {} %}

{# Loop all entries #}
{% for key, entry in allEntries %}

    {# Set conditionals to filter for desired entries #}
    {% if ( entry.startDate >= unixbegins and entry.startDate < unixends ) and ( entry.endDate >= unixbegins and entry.endDate < unixends ) %}

        {# Merge entries #}
        {% set filteredEntries = filteredEntries|merge({ (key): entry }) %}
    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: Error: The merge filter only works with arrays or hashes. Both entriesStartDate and entriesEndDate return a llist of entries correctly.  
> **username_0**: Actually, I don't want merge, do I? I only want entries which in both entriesStartDate and entriesEndDate.  
> **username_1**: Hmm, you wrote "with either a start date OR and end date within a calendar year" that means all entries with a start date in that year PLUS all entries with an end date in it.  
> **username_0**: Yeah, you're right. I do need merge. As long as they aren't repeated.  
> **username_0**: Yep, the new improved merge solved it. Thanks.  
> **username_1**: That's good to hear, @Clive. I'll delete that old code for clarity.  
> **username_0**: Just so you know, there's something in lines 2 and 3 of your first answer which knackers a Bootstrap column for Webkit browsers. I don't know quite where is was, but typing them out rather than pasting them fixed it.  

---

