Date/time Views filter tries strotime() relative to Unix epoch

Created on 9 January 2016, almost 9 years ago
Updated 10 August 2024, 4 months ago

Problem/Motivation

Creating a View to list content with a datetime field, filtered by date < today, caused no results to be displayed.

In \Drupal\views\Plugin\filter\Date,

The date input is parsed with strtotime(), which will attempt to turn anything into a date. The input isn't validated, so you can enter an absolute or relative date in either.

There are 4 usages depending on the date type "date" or "offset" when comparing string based dates.

  • Case 1: Enter an absolute date w/ type=='date', the date gets turned into a timestamp and used directly.
  • Case 2: Enter a relative/offset date w/ type=='date', the date gets turned into a timestamp and used directly. If you did something w/o a reference, then the offset gets added to 1970/01/01, and used.
  • Case 3: Enter an absolute date w/ type=='offset', the date gets turned into a timestamp and user directly; with strtotime() when you give it an absolute date, the second parameter gets ignored.
  • Case 4: Enter a relative/offset date w/ type=='offset', and the input gets converted into a second delta from 1970/01/01 and then added the the current time as part of the query.

Cases 1 and 4 match the text on the UI, and work as expected. Cases 2 and 3 are really undefined conditions per the text on the UI.

Proposed resolution

Update views filter plugin for DateTime to always use the current request time instead of using a SQL expression ***CURRENT_TIME***' . sprintf('%+d', $a) that gets parsed later.

This resolves relative/offset filtering for both datetime and timestamp-based fields such as "created" or "changed".

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
ViewsΒ  β†’

Last updated about 6 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom pjcdawkins

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024