Filter views by comparing two fields?

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.

Views Developers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: