After searching far and wide the web community for an event filter between two dates I decided to write this tutorial to help others like me who had a little challenge finding how to do it.

It is tempting to believe that using the "is between" on a date fields will work. However, this option is used only in the case of events "Starting" on a rage of date.

This module (https://www.drupal.org/project/views_between_dates_filter) is used to filter evenst on ONE date and NOT a range of date (as the image suggest).

The following conversations have guided me to this tutorial.
https://www.drupal.org/node/1302940
http://drupal.stackexchange.com/questions/36380/drupal-7-views-filter-be...

This tutorial requires that you already have a content type with a date field . It is not necessary to have two fields to adjust an "end date" for the event, only a date field with the "Collect an end date."

This part (which seems "logical" to start - but it is not so much) is the basis of this discussion.
Take for example this figure, based on the discussion of https://www.drupal.org/node/1302940
figure1

A search filter between the 1th and 31th of July should provide us the events A, B, C and D (shaded area), and Exclude E and F.

If we have a filter with a Date field (based on the Start date) using the option "is between" will only show B and C since their "start date" is between those dates, but will exclude events A and D since their "start date" is before the desired search . This is not the desired result, we want to include them in the results.

The following conversation https://www.drupal.org/node/1302940 explains:

event end date >= user start date
AND
event start date <= user end date

Following this, we must swap the "Start date" and "End date" to the users. Users will not notice but it is crucial to obtaining the desired results.

To include events A and D in this search, we will need two filters exposed to our Views (again: based on the same field and not two separate fields)
In this order:

1. Date - end date (field_dates:value2)
2. Date - start date (field_dates)

In " Date - end date (field_dates:value2)", Select popup as form element,
Figure2

Next window:
Exposed this filter to visitor

Even though this label says "End date", we are going to make believe this is the "Start date", so change this label to "Start Date" instead.
figure3

Change the operator to "Is greater than or equal to", leave the "Select a date" as is. (event end date >= user start date)
figure4

Do the same to the second filter:

Date - start date (field_dates)
- change this label to "End Date",
- Select popup as form element,
- Exposed this filter to visitor

This time Change to operator to "Is less than or equal to", leave the "Select a date" as is. (event start date <= user end date)

Last suggestion: Make sure that you have "AND" between those two operators.

figure5

Save everything, Enjoy...

Hope this helps someone along the Drupal mystical path.. Sorry for any typo or grammatical mistake, English is not my native language.

Feel free to comment or correct me, I'll adjust.

CommentFileSizeAuthor
figure5.gif20.6 KBkaribel
figure4.PNG7.16 KBkaribel
figure3.PNG3.62 KBkaribel
figure2.PNG3.28 KBkaribel
Figure1.gif8.71 KBkaribel

Comments

aniket.mohite88’s picture

Hi Mory,

Thanks. Absolutely grateful to u. This was exactly what I needed. Seriously, needed a solution to my need.
This even works with the Facet API search build up.

Note: The first image helped to understand my scenario quickly.

A picture is worth a thousand words

Thanks a lot agian.

karibel’s picture

Great, I'm happy it helped someone, because it took me a long time to figure it out :-)

pmoz’s picture

Thanks for the write up, but I'm still trying to get this to work in our use case.

I think where I am getting tripped up are dates that don't have an end date.

Any ideas?

karibel’s picture

pmoz,
I'd say that this method will also give you events that don't have an end date, because this show you ALL events happening BETWEEN those 2 dates. It doesn't need an end date to appear in the results.

pmoz’s picture

Thanks for the input Mory.

This is what worked in our case:

Start Date <= Y (changed exposed filter label to End Date)
and
End Date >= X (changed exposed filter label to Start Date)
or
End Date is Null

The null was wreaking havoc on us.

Don't know if it matters, but we are filtering a civicrm date.

nikitas’s picture

great! thank you!

umberto.’s picture

Thank you!!!

aniket.mohite88’s picture

Hi pmoz,

May be u can add a default end date, with respect to the start date that you enter.
Please refer below resources

Or may be you can write a custom code for your node-form to work this way using hook_form_alter()

darvanen’s picture

Version: 7.x-2.7 » 7.x-2.8
Status: Active » Reviewed & tested by the community

Excellent documentation, thank you.

Now how do we get it included in the module documentation instead of the issue queue? I can edit existing pages but not create new ones.

cafuego’s picture

I think you create a new book page via https://www.drupal.org/node/add/book and then ask someone on the doc team and ask them to move it under the Site Building Guide - Date/Calendar.

karibel’s picture

Priority: Normal » Minor
Status: Reviewed & tested by the community » Closed (fixed)

Thank you cafuego I just did. I learned something today, Yay

https://www.drupal.org/node/2507601

cafuego’s picture

Nice work! :-)

aniket.mohite88’s picture

Nice. Helpful.

caspianroach’s picture

The swap around part blew my mind. It made everything so easy! And to think I was ready to start hacking at the filters to produce nested filter groups...

awolfey’s picture

A comment on pmoz's situation: A Date module date field will set the end date to the to date when you don't explicitly set it to something else. I'm not sure what the civicrm field does.

aruuska’s picture

Thanks for article.
I have strange issue here. End date exposed form field granularity looks like "month" even I change it to any other options. When I select date in popup calendar in field show up e.g. "2015-09"

When I printed out form in _form_views_exposed_form_alter function, end date field looks like this:
field_event_date_value (Array, 2 elements)
#tree (Boolean) TRUE
value (Array, 10 elements)
#title (String, 0 characters )
#type (String, 10 characters ) date_popup
#size (Integer) 20
#default_value (String, 10 characters ) 2015-09-30
#date_format (String, 5 characters ) d.m.Y
....

Do I miss something here? Any ideas how I can get date selected in end date as well? :)
EDIT:
Sorry about this :) I'm new developer in project and there is override for date field format in some form_alter.
Thanks again for great article.

karibel’s picture

aruuska,
In your Exposed filter "Parameter" (The end date parameter) do you have "day" selected as your Granularity?

aruuska’s picture

mory,
Yes, everything was okey, but there was custom override code in one module. :) so, problem was "project specific" :)
Your post was very helpful for me, thanks!

rcodina’s picture

Anyone knows how to do this on Search API views? Sadly, module "Views between dates filter" doesn't work on Search API views...

priyanka12chavan’s picture

This is really very helpful!! Thank you. It hardly took 5 minutes to set it right. You saved my day.

RomainD’s picture

Hello there ! Thank you so much for this tutorial, it helped a lot.
However one point is missing : what if I have an event that starts the first of july to the 15th and I only want to see the last 7 days of July.
Then I will make a filter from the 24th to the 31th for example.
But my event will appear because his end date is greater than 15 and is start day is less than 31 while is not inside the frame that we have set.

Or maybe I am missing something !

Any idea to fix this ?

giupenni’s picture

Great tutorial, save me a lot of time!

sunil khan’s picture

Hello Everyone,

I need a little help. Please someone help me
My issue is that i have many news of different Month. I want to create one content type and many views and want to apply filter on date So that every news is place in their relavent month folder. Here a example

News (page)

2016
December
Nov
Oct
.
.
.

2015
Dec
.
.
.
so on

if i click on dec under 2016 then news of dec2016 opens.

Please comment if anyone have some idea.

karibel’s picture

@sunil khan

Take a look a this conversation, it should help you.
https://www.drupal.org/node/388140

aniket.mohite88’s picture

Hi Sunil Khan,

For your requirement, I think you should use calendar module.

You can refer the following tutorial for your month view of results -
Drupal 7 Calendar Module - Daily Dose of Drupal Episode 155

Basically for you, the catch will be the following -

  1. For your content-type, set the proper url pattern (admin/config/search/path/patterns). say news/[node:title]
  2. In your calendar View, for the "Month" view display, you should change the display format from calendar to list (html or un-formatted)
  3. Now check the url structure of the month view. Its outputs in the format /month/2016-02.
    You will have to create a static block (admin/structure/block/add) by yourself where you create links months wise, as per your requirement.
    Example- siteurl/calendar/month/2016-02 for FEB 2016, siteurl/calendar/month/2016-01 for JAN 2016, siteurl/calendar/month/2015-12 for DEC 2015 & so-on
    You can then place this block on your configured URL pattern in pt.1 - i.e. siteurl/news/* or any other page

I think, this is what you are actually looking for. Hope this helps.

Regards,
Aniket Mohite

agilman’s picture

The original tutorial worked like a charm. No additional modules to install. It all makes sense as you're configuring it.

aris.rep’s picture

morybel your post was very clear and helpful, thanks!

joekers’s picture

Thanks for this great tutorial - it really helped a load!

jason@blaze’s picture

Thanks a bunch for this, saved me a ton of time. =)

nor sairi’s picture

thanks , help me a lot

othermachines’s picture

Wow, a 3-year-old D7 tutorial just helped me get a date range filter up and running on D8 in about 5 minutes. Not a bad start to the day.

Thanks tons, @morybel!

Grooo’s picture

Thanks so much.
A very helpful explain.