How to search a custom date field by year
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
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?
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:
?
Low 28 May 2013 12:49
Something like that, yes. Separate the name and values like this: