With a facet on a Date field, the facet's displayed label for the Month+Year visualization is wrong for some months.

Mistaken months are those with less than 31 days: February, April, June, September, November, and the next month's label for all these.
For instance, April 2014 becomes May 2014, June 2014 becomes July 2014, and so on.

I could repeat the error in a clean installation with search api views, Solr and Db indexes.
Repeated also with all three different date formats.

I think it's a presentation issue since the underlying values seem ok: the count is right, and further refining the search the label of the month is correct (see the screenshots attached).

Thanks
Filippo

Comments

filippo.ledda’s picture

Title: Wrong date labels are displayed for 30-days months » Wrong date labels are displayed for less than 31-days months
Project: Facet API » Search API
Component: Code » Facets
Issue summary: View changes

It seems it's a DateTime::createFromFormat bug, or at better, very strange behavior.
The conversion is made in the function search_api_facetapi_map_date.

Using
DateTime::createFromFormat("Y-m", "2014-02")

We expect a date where year is 2014 and month is February. But the date has also the current day and time.
So, if today is 31, internally is set 2014-02-31, that is converted back to the timestamp of March 3.

I can't think anything better than bypass the function when this format is involved. Any idea?

filippo.ledda’s picture

Version: 7.x-1.5 » 7.x-1.13
filippo.ledda’s picture

Assigned: Unassigned » filippo.ledda
Status: Active » Needs review
StatusFileSize
new1.09 KB

strtotime seems to solve the issue.

Attached the patch.

Status: Needs review » Needs work

The last submitted patch, 3: search_api_facetapi-wrong_date_month_labels-2313291-3.patch, failed testing.

rovo’s picture

I had the same issue, and this patch fixed it for me. I applied it manually, leaving in the $format granularity format line (line 543). I wasn't sure why that line was removed.

- $format = search_api_facetapi_date_get_granularity_format($granularity);

Thank you.

filippo.ledda’s picture

Hi, the line has been removed because the variable $format is no longer needed.

switzern’s picture

Version: 7.x-1.13 » 7.x-1.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.1 KB

This fix also worked for me to solve the same problem. I've re-rolled the patch to apply to the tip of the Search API repo, per the SimpleTest feedback in the prior version.

manuel garcia’s picture

Assigned: filippo.ledda » Unassigned
Status: Needs review » Reviewed & tested by the community

Confirming both the bug, and that the patch fixes it.

hartsak’s picture

Ok, I realize this is somewhat old issue but this issue happened to me today. It's 31st day of this month and the date filter was working fine before this day on this month. I tested the patch and the date filter is working perfectly now! I didn't go through the code but I assume the day of the month when the date filter is viewed could affect this making testing more difficult?

This is how the date filter looks like before the patch:

<select class="facetapi-select form-select" id="edit-facets" name="facets">
  <option value="" selected="selected">-- Choose --</option>
  <option value="/events/date/2016-04"> May 2016 (4)</option>
  <option value="/events/date/2016-05"> May 2016 (4)</option>
</select>

And after the patch:

<select class="facetapi-select form-select" id="edit-facets" name="facets">
  <option value="" selected="selected">-- Choose --</option>
  <option value="/events/date/2016-04"> April 2016 (4)</option>
  <option value="/events/date/2016-05"> May 2016 (4)</option>
</select>
drunken monkey’s picture

Status: Reviewed & tested by the community » Closed (duplicate)

Sorry I didn't see this back then, but it's now already been fixed in #2678856: Date facets show wrong month on leap days.

manuel garcia’s picture

@drunken monkey thanks for the heads up!