×

Filter content between Date ranges in Drupal 7

Read Article
Pause
Resume
Stop
Filter content between Date ranges in Drupal 7

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 range of date.

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.

A search filter between the 11th 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:

  • Date - end date (field_dates:value2)
  • Date - start date (field_dates)

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

In next window: Select 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.

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

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)

And one more thing, Make sure that you have "AND" between those two operators.

Save the view.

Note : While you are working with date module, and filtering the content between dates, make sure that your Server timzone, Mysql server timezone and Drupal site timezone are same. If not you may face problems in few cases.

Thank you :)

Comments

Scroll to Top
We Need Your Consent
By clicking “I Accept Cookies”, you agree to the storing of cookies on your device to enhance site navigation & analyze site usage.
I Accept Cookies