All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

How to search a custom date field by year

atlelill 28 May 2013 10:44 question, complete

I have entries with 2 custom date fields, a start date and an end date. I have a search form with several inputs, and I want the visitor to be able to "Search for entries with start date 2009" and/or "end date 2014" in two separate input fields. Do I need to date-format my input fields or something to make it work?

Replies

  1. Low 28 May 2013 11:21

    Searching dates is tricky. EE stores dates as a Unix timestamp (a big number). To be able to filter by year, you're actually saying: all entries between 2 numbers (Jan 1st 2009 to Dec 31st 2009). And that's just for a start date.

    So, if you're targeting 2 date fields, you actually need 4 values: one range with from/to values for the start date, one range with from/to values for the end date.

    You will need to use the range:-syntax instead of the native search:-syntax, as the latter will only search for exact matches.

    Make sense?

  2. atlelill 28 May 2013 12:46

    I'm not afraid to write a few extra lines of code ;-)

    So, in my search-form I should write something like:

     
    <h3>Year</h3>
    <input type="text" name="range:my_date_field_a="2000-01-01;2020-01-31"" value=""><label>Start year</label>
    <input type="text" name="range:my_date_field_b="2000-01-01;2020-01-31"" value=""><label>End year</label>


    ?

  3. Low 28 May 2013 12:49

    Something like that, yes. Separate the name and values like this:

    <input name="range:my_date_field_a" value="2000-01-01;2020-01-31"> 
    <input name="range:my_date_field_b" value="2000-01-01;2020-01-31">