Steps to reproduce:
Add filter for node creation date, select 'Between' operator and type something like

first day of last month
last day of last month

It will be interpreted as

AND (node.created >= -2678400) AND (node.created <= -86400)

because there is a base for calculating as relative date in op_between() function in handlers/views_handler_filter_date.inc:

  function op_between($field) {
    if ($this->operator == 'between') {
      $a = intval(strtotime($this->value['min'],0));
      $b = intval(strtotime($this->value['max'],0));
    }
    else {
      $a = intval(strtotime($this->value['max'],0));
      $b = intval(strtotime($this->value['min'],0));
    }

It is fixed for me if I delete these zeros:

  function op_between($field) {
    if ($this->operator == 'between') {
      $a = intval(strtotime($this->value['min']));
      $b = intval(strtotime($this->value['max']));
    }
    else {
      $a = intval(strtotime($this->value['max']));
      $b = intval(strtotime($this->value['min']));
    }

These zeros also present in 6.x-3.0-alpha3.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Can you please have a look at see whether it works for you in the dev versions?

artis’s picture

Version: 6.x-2.12 » 6.x-3.x-dev
Status: Active » Needs review
FileSize
0 bytes

patch against 6.x-3.x-dev

artis’s picture

Version: 6.x-3.x-dev » 7.x-3.x-dev
FileSize
665 bytes

patch against 7.x-3.x-dev

dagmar’s picture

Status: Needs review » Needs work

Nice catch. I tested your patch and works as expected. However reading the code of views_handler_filter_date.inc I found this:

  function op_simple($field) {
    $value = intval(strtotime($this->value['value'], 0));

And after test the same "first day of last month" I noticed that it doesn't work as expected.

Please provide a patch containing the fix for op_simple too. Thanks.

dawehner’s picture

Status: Needs work » Needs review
Issue tags: +Needs tests

Changing the base of strtotime is also not right because this would kill a lot of other things.

The filter is not designed for expecting this kind of input if you use "A date in any machine readable format."

Maybe a third value type could be introduced to make it a bit easier to understand.

If this is fixed it should be simpletested as well. Updating the status, so the testbot is runned here.

Status: Needs review » Needs work

The last submitted patch, dynamic_dates-1182256-3.patch, failed testing.

das-peter’s picture

Atm. I can't imagine a scenario in which I need the base 0 for the relative date calculation. For me it seems 0 is used to be able to work with ***CURRENT_TIME*** for offset values.
But what if we change to code to something like this:

function op_between($field) {
    $query_substitutions = views_views_query_substitutions($this->view);
    $a = intval(strtotime($this->value['min'], $query_substitutions['***CURRENT_TIME***']));
    $b = intval(strtotime($this->value['max'], $query_substitutions['***CURRENT_TIME***']));

That way offset values rely on $query_substitutions['***CURRENT_TIME***'] and in the normal case $query_substitutions['***CURRENT_TIME***'] is REQUEST_TIME - which means a value like now gets the current timestamp and not 0.

das-peter’s picture

Status: Needs work » Needs review
FileSize
1.98 KB

After a short chat with Daniel I decided to create a patch and see if all current tests pass ;)
Values like first day of last month / last day of last month should now return a usable timestamp.
Feedback very welcome!

tim.plunkett’s picture

Status: Needs review » Needs work

So this doesn't break any existing tests, but it definitely needs new ones before being committed.

das-peter’s picture

I tried to remember why I had to change this but I can't reproduce it anymore :|
The current code will produce {field} > {CURRENT_TIME}+0 for now as long as you define the value as an offset.

   $value = intval(strtotime($this->value['value'], 0));
    if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
      $value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign
    }
    $this->query->add_where_expression($this->options['group'], "$field $this->operator $value");

Only "advantage" of the patch would be a potentially easier to understand construct because it removes to need to handle "offset" values in a special way.
This also means that the UI could be adjusted because there's no need to declare the type of the date.

... but it definitely needs new ones before being committed.

What kind of new tests? As far as I can see the coverage is quite good for this.

windmaomao’s picture

I had the same issue, seems to me the bug is because

  strtotime('last mon', 0) 

doesn't return the correct interval

#8 works for me !!

jbenezech’s picture

Same problem here trying to filter node created between today and tomorrow. #8 worked for me

stella’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
4.38 KB

Updated patch, no changes other than the addition of some new tests

Status: Needs review » Needs work

The last submitted patch, 13: 1182256-13-relative_date_filter.patch, failed testing.

stella’s picture

Status: Needs work » Needs review
FileSize
3.98 KB

Patch reroll against latest dev

mariancalinro’s picture

Status: Needs review » Reviewed & tested by the community

I tested the patch in #15, and it fixes the issue on both between and simple conditions.

fonant’s picture

Patch in #15 works here so that "midnight" results in the previous midnight, whatever time of day it is currently, as you'd expect from a php relative date.

I haven't done exhaustive testing, but this seems to be quite an important fix. Without it common relative date strings like "yesterday" and "today" just don't work the way they work in PHP's strtotime().

vadym.kononenko’s picture

I've tried to add views filter between two dates: 1) 'Last Monday -1 week', 2) 'Last Monday' to get entities for previous week.

Generic views behaviour calculates for me wrong timestamps. It became shifted and instead of get two mondays return me just thursdays.

I think it is because strtotime uses calendar for 01.01.1970 day and it produces an logical error.
Patch #15 works for me. Thank you a lot!

olli’s picture

+++ b/handlers/views_handler_filter_date.inc
@@ -172,10 +170,10 @@ class views_handler_filter_date extends views_handler_filter_numeric {
-      $value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign
-    }
+    // Use the substitutions to ensure a consistent timestamp.
+    $query_substitutions = views_views_query_substitutions($this->view);
+    $value = intval(strtotime($this->value['value'], $query_substitutions['***CURRENT_TIME***']));

I think the ***CURRENT_TIME*** is needed for caching so we can't replace it here, right?

fonant’s picture

Would be really nice to get this patch into a supported release. The latest Views security release doesn't include this important fix, so the patch has to be re-applied.

This fixes a nasty bug that's difficult to spot and diagnose if you're using relative date filters in Views!

dawehner’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 7.x-3.x-dev » 8.0.x-dev
Component: Code » views.module
Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: +VDC

@olli
I think the query cache doesn't change. The following code generates parts of the result key:

        // the query objects.
        if ($build_info[$index] instanceof Select) {
          $query = clone $build_info[$index];
          $query->preExecute();
          $build_info[$index] = (string)$query;
        }

This piece of code triggers the query_alter() hook and based upon that always had the ***CURRENT_TIME*** replace.
This means that those queries are already not cacheable and you better write your own cache plugin, if you need them to be. (I hope all this is true)

Given that this issue solves a bug, let's get it in. It would be great if someone could evaluate whether we need this in 8.0.x as well.

Committed to 7.x-3.x and pushed

jibran’s picture

Status: Patch (to be ported) » Needs review
Issue tags: +Needs tests
FileSize
1.99 KB

Here is the patch without tests.

dawehner’s picture

That part seriously needs tests indeed but the port is looking fine so far!

kenorb’s picture

Related issues: +#1932078: Better date filter
Lendude’s picture

The last submitted patch, 26: date_filter_does_not-1182256-26-TEST-ONLY.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

PCate’s picture

Ran into this issue today. I think the patch needs a re-roll.

Yasser Samman’s picture

Re-roll.
This is kind of an important issue I don't know why it's being ignored.

Status: Needs review » Needs work

The last submitted patch, 35: 1182256_35.patch, failed testing. View results

Yasser Samman’s picture

FileSize
3.69 KB

Missed including views.views_execution.inc file to be able to use views_views_form_substitutions

PCate’s picture

Thank you @Yasser Samman for the patch re-roll. I applied it and it fixed the issue.

PCate’s picture

Status: Needs work » Reviewed & tested by the community
Lendude’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for working on this! One thing I see though:

+++ b/core/modules/views/src/Plugin/views/filter/Date.php
@@ -166,15 +166,12 @@ public function acceptExposedInput($input) {
+    \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.views_execution');
+    $query_substitutions = views_views_query_substitutions($this->view);

@@ -182,11 +179,11 @@ protected function opBetween($field) {
+    // Use the substitutions to ensure a consistent timestamp.
+    \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.views_execution');
+    $query_substitutions = views_views_query_substitutions($this->view);

views_views_query_substitutions() is a hook implementation so we shouldn't be calling that directly.
We should probably \Drupal::moduleHandler()->invokeAll('views_query_substitutions', [$this->view]); (or better yet inject the moduleHandler in the plugin if possible)

shubham.prakash’s picture

Status: Needs work » Needs review
FileSize
3.77 KB

Did the changes suggested in #41

shubham.prakash’s picture

FileSize
2.96 KB
Lendude’s picture

@shubham.prakash thanks for the quick pick up, couple of things:

  1. +++ b/core/modules/views/src/Plugin/views/filter/Date.php
    @@ -166,15 +166,12 @@ public function acceptExposedInput($input) {
    +    \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.views_execution');
    
    @@ -182,11 +179,11 @@ protected function opBetween($field) {
    +    \Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.views_execution');
    

    We can lose the includes when using the module handler to invokeAll

  2. +++ b/core/modules/views/src/Plugin/views/filter/Date.php
    @@ -166,15 +166,12 @@ public function acceptExposedInput($input) {
    +    $query_substitutions = \Drupal::moduleHandler()->invokeAll('views_query_substitutions', [$this->view]);
    
    @@ -182,11 +179,11 @@ protected function opBetween($field) {
    +    $query_substitutions = \Drupal::moduleHandler()->invokeAll('views_query_substitutions', [$this->view]);
    

    Did you try using Dependency Injection (like I hinted at in #40) by implementing ContainerFactoryPluginInterface on the plugin instead of using \Drupal? If not, that would be the preferred way of doing this, unless there is some reason not to do this.

Yasser Samman’s picture

FileSize
5.47 KB

Thanks @Lendude.
Using dependency injection to be able to use module_handler service.
I just didn't have enough time the last time I did it :p

Status: Needs review » Needs work

The last submitted patch, 44: 1182256_44.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Yasser Samman’s picture

Ok..
So the test failed because the datetime module is extending our Drupal\views\Plugin\views\filter\Date that we modified. Possibly other modules are extending the same plugin too.
What do you think we should do? Should we keep using \Drupal::moduleHandler() insead of dependency injection for the time being?

Version: 8.7.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

marcvangend’s picture

Status: Needs work » Closed (duplicate)

Closing this in favor of #2647292: Date/time Views filter tries strotime() relative to Unix epoch. The solution in that issue is almost identical to the patch here. Reviews are appreciated.