I want to highlight a tablerow when a datefield is less then 6 months from now.

I tried

if ($node_data_field_maturity_field_maturity_value < "now +6 months") return 'red';

but that leaves al rows uncolored, using the ">" instead colors all rows red.

The "now +6 months" does work when used in a views filter.

Does the module support these kind of selections?

Comments

smokris’s picture

You've almost got it.

Right now your code is comparing two strings --- an ISO representation of the datefield value, and the literal string "now +6 months". To PHP this is by default just an undifferentiated string.

You can use the PHP strtotime() function to convert both the datefield value and the "now +6 months" relative date specifier into numeric time representations, which PHP can then sensibly compare.

Try this:

if (strtotime($node_data_field_maturity_field_maturity_value) < strtotime("now +6 months")) return 'red';
Argus’s picture

This works fine indeed (thanks!), but unfortunatly only in the Live Preview. Once I save the view it returns all rows red.

This may be another issue though for I also noticed the same happening on another occasion. FYI I have the module working fine in yet another view.

smokris’s picture

Status: Active » Fixed

Closing inactive support request.

lunk rat’s picture

Status: Fixed » Active

I used this same snippet and I also have the problem that it works great only in live preview, and only on the "Defaults" preview. When I preview the "Page", highlighting does not appear. There is no highlighting at all on the actual view, once the view is saved.

I also have highlighting working in a dozen other views on the same site. But with this particular date highlighting, I can only get it to work in the Defaults live preview in views UI.

smokris’s picture

Status: Active » Postponed (maintainer needs more info)

Hmm. I just created a test site, added a "Date" field to a content type, added a couple sample nodes, and created a view using the above snippet. Highlighting works for me both in preview and on the actual View page.

@LunkRat: please export your Content Type and your View (using Features), and attach the module to this issue, so I can try to recreate your exact scenario. (Or provide a set of instructions for reproducing this issue from a clean test site.)