Filter views by comparing two fields?

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
wipeout_dude's picture

Hi,

I have a content type with two date fields both with start and end dates..

I need to create a view filter that if the start date and time of fieldA is more than 15 minutes before or after the start date and time of FieldB then show it in the view..

Obviously views can compare dates to "now" or now relative to a time period but I can't see a way for views to filter based on the comparison of the value of two fields..

Anyone have any ideas?

Thanks..

Comments

I used the Views PHP module

alekth's picture

I used the Views PHP module (http://drupal.org/project/views_php) for a filter of that sort (though not time based). You will have a Global: PHP filter option where you get access to the variables of your view's fields, and from there you can compare them (if code returns TRUE, field is shown).

Assuming your dates are Unix timestamps, you can subtract the two, and compare whether their absolute value is more than 15*60 (seconds).
e.g. if your fields are timestamp1 and timestamp2, you would have something along the lines:

$diff = abs($row->timestamp1 - $row->timestamp2);
return ($diff >= 900);

(I don't really do PHP, so hopefully this is correct).

declaring a reference the date field

maliky's picture

You could try to declare a hidden date field before the one you want to display.
The hidden and the visible date field will have the same values obviousely but, you should be able to access the first (hidden) date field values with tokens in the configuration of your second (visible) date field.

I'm not sure if this can work but, if it does the hidden date field should be placed before the visible one in your view filter.

If time is money then those who have time are rich !

You could use computed_field,

Len Porcano's picture

You could use computed_field, http://drupal.org/project/computed_field, to populate a field with the time difference when the content is created/edited. This would leave you a single field to filter by in views, and would mean the calculation is only done once at save and not each time the view is created.

it is now included in views

CProfessionals's picture

it is now included in views 7.x-3.7+12-dev

maddentim's picture

No need to install the dev version any longer and if you did, you can update safely to stable 3.8 instead (as far as this issue goes :)

Filter criterion "Global: Fields comparison"

jlstrecker's picture

This functionality is added to Views module: http://drupal.org/node/699252#comment-7513087

To clarify: When picking a filter criterion, use "Global: Fields comparison". This lets you pick the two fields to compare for equality, inequality, etc. (Screenshot on comment #64 on the issue.)

I tried the Global Fields comparison

Games 4 Leaders's picture

...but it doesn't work. It seems to compare the items individually, so if the user has nails, but the job requires hammer AND nails, it has 2 records in the table like this:
Title | The job needs | User inventory
One | Hammer | Nails
One | Nails | Nails

so what it does is filter out the first one and allow the second one. What I need it to do is compare them all like this:
Title | The job needs | User inventory
One | Hammer, Nails | Nails

in which case it would filter it out.

+1 No following in groups :(

nithinkolekar's picture

+1
No following in groups :(