I followed your instructions here - https://drupal.org/node/2005066 but I do not ever get desired results.

If I choose date less than I get a yes, if I choose greater than I get a no. I've tried using DATE_UNIX and DATE_STAMP.

I have dates which are less than, and great than but all results are either yes or no. I've exported my view.

Thanks

$view = new view();
$view->name = 'dashboard_items';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Dashboard Items';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Dashboard Items';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '50';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Field: Content: Body */
$handler->display->display_options['fields']['body']['id'] = 'body';
$handler->display->display_options['fields']['body']['table'] = 'field_data_body';
$handler->display->display_options['fields']['body']['field'] = 'body';
$handler->display->display_options['fields']['body']['label'] = 'Notes';
/* Field: Content: Edit link */
$handler->display->display_options['fields']['edit_node']['id'] = 'edit_node';
$handler->display->display_options['fields']['edit_node']['table'] = 'views_entity_node';
$handler->display->display_options['fields']['edit_node']['field'] = 'edit_node';
/* Field: Content: Install/Buy Date */
$handler->display->display_options['fields']['field_install_date']['id'] = 'field_install_date';
$handler->display->display_options['fields']['field_install_date']['table'] = 'field_data_field_install_date';
$handler->display->display_options['fields']['field_install_date']['field'] = 'field_install_date';
$handler->display->display_options['fields']['field_install_date']['settings'] = array(
'format_type' => 'short',
'fromto' => 'both',
'multiple_number' => '',
'multiple_from' => '',
'multiple_to' => '',
);
/* Field: Content: Service Date/Expire Date */
$handler->display->display_options['fields']['field_service_date']['id'] = 'field_service_date';
$handler->display->display_options['fields']['field_service_date']['table'] = 'field_data_field_service_date';
$handler->display->display_options['fields']['field_service_date']['field'] = 'field_service_date';
$handler->display->display_options['fields']['field_service_date']['settings'] = array(
'format_type' => 'short',
'fromto' => 'both',
'multiple_number' => '',
'multiple_from' => '',
'multiple_to' => '',
);
/* Field: Views: Views Conditional */
$handler->display->display_options['fields']['views_conditional']['id'] = 'views_conditional';
$handler->display->display_options['fields']['views_conditional']['table'] = 'views_conditional';
$handler->display->display_options['fields']['views_conditional']['field'] = 'views_conditional';
$handler->display->display_options['fields']['views_conditional']['label'] = '';
$handler->display->display_options['fields']['views_conditional']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['views_conditional']['if'] = 'field_service_date';
$handler->display->display_options['fields']['views_conditional']['condition'] = '4';
$handler->display->display_options['fields']['views_conditional']['equalto'] = 'DATE_STAMP';
$handler->display->display_options['fields']['views_conditional']['then'] = 'yes';
$handler->display->display_options['fields']['views_conditional']['or'] = 'no';
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
'dashboard_items' => 'dashboard_items',
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'dashboard-items';

/* Display: List */
$handler = $view->new_display('page', 'List', 'page_1');
$handler->display->display_options['path'] = 'dashboard-list';

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ikaros123’s picture

Version: 7.x-1.0 » 7.x-1.1

Date comparison is not working for me either... (Views is 7.x-3.5)
I also followed the instructions here https://drupal.org/node/2005066

If this field ....
is Less than DATE_STAMP
then output "......"
otherwise output "....."

no mater what the date is , the "Otherwise output" is always displayed

IWasBornToWin’s picture

Priority: Normal » Major

Is this module maintained?

vavethecoder’s picture

I am new to drupal, so I don't know how to reply...

For Date comparison I made some change to views_conditional_handler.inc file line no 221 to 239 and now it's working for me.

These are the changes. May be this will help you.


    // If we find a date stamp replacement, replace that.
    if (strpos($equalto, 'DATE_STAMP') !== FALSE) {
      $date_equalto = explode(" ", str_replace('DATE_STAMP', format_date(REQUEST_TIME), $equalto));
      $equalto = strtotime($date_equalto[1]);
    }
    
    if (strpos($then, 'DATE_STAMP') !== FALSE) {
      $then = str_replace('DATE_STAMP', format_date(REQUEST_TIME), $then);
    }
    
    if (strpos($or, 'DATE_STAMP') !== FALSE) {
      $or = str_replace('DATE_STAMP', format_date(REQUEST_TIME), $or);
    }

    // If we find a unix date stamp replacement, replace that.
    if (strpos($equalto, 'DATE_UNIX') !== FALSE) {
      $equalto = str_replace('DATE_UNIX', format_date(REQUEST_TIME), $equalto);
    }
    
    if (strpos($then, 'DATE_UNIX') !== FALSE) {
      $then = str_replace('DATE_UNIX', REQUEST_TIME, $then);
    }
    
    if (strpos($or, 'DATE_UNIX') !== FALSE) {
      $or = str_replace('DATE_UNIX', REQUEST_TIME, $or);
    }

    // Strip tags on the "if" field.  Otherwise it appears to
    // output as <div class="xxx">Field data</div>...
    // ...which of course makes it dificult to compare.
    if(strpos($fields["$if"]->last_render, "datatype=\"xsd:dateTime\"") !== FALSE){
        $date_if = explode(" ", trim(strip_tags($fields["$if"]->last_render)));
        $r = strtotime($date_if[1]);
    } else {
        $r = trim(strip_tags($fields["$if"]->last_render, '<img>'));
    }

thecoalman’s picture

I got this working using the code above with one minor change but I'm only using the Unix time stamp. The format for the date in the view is plain.

This was giving me a php error so I didn't use it, didn't write it down but "unexpected xsd" or something:

    if(strpos($fields["$if"]->last_render, "datatype="xsd:dateTime"") !== FALSE){
        $date_if = explode(" ", trim(strip_tags($fields["$if"]->last_render)));
        $r = strtotime($date_if[1]);
    } else {
        $r = trim(strip_tags($fields["$if"]->last_render, '<img>'));
    }

This:

    // If we find a unix date stamp replacement, replace that.
    if (strpos($equalto, 'DATE_UNIX') !== FALSE) {
      $equalto = str_replace('DATE_UNIX', format_date(REQUEST_TIME), $equalto);
    }

Needs to be:

    // If we find a unix date stamp replacement, replace that.
    if (strpos($equalto, 'DATE_UNIX') !== FALSE) {
      $equalto = str_replace('DATE_UNIX', REQUEST_TIME, $equalto);
    }
svanou’s picture

Hi !
I'm trying to write into the code but not working for me.
Can you guide me in the right direction ?

From log:
PHP Parse error: syntax error, unexpected 'xsd' (T_STRING)

Thanks!

ianniscamillieri’s picture

Same trouble : can't compare a date field with DATE_STAMP using Views Conditional in Views 7.x-3.7.
I've tried some of the code above but get T_STRING error.
Does anyone have an other workaround? Or heard about this new feature in a next release?
I think the should be a must have working feature for that module.

HappyPanda’s picture

Thanks for everyone's suggestions. I do love the Drupal community.

I was having similar problems. In my case trying to compare a date field (formatted to display in UNIX epoch time), with the current time (DATE_UNIX in this module).

Turns out the code doesn't currently set $equalto to current date, just leaves it as the string 'DATE_UNIX'... so any comparison using DATE_UNIX will be useless.

Adding the following around line 237 in views_conditional_handler.inc fixed it for me:

// If we find a unix date stamp replacement, replace that. Note that this is in UNIX epoch
// Change your REQUEST_TIME call to get other formats
if (strpos($equalto, 'DATE_UNIX') !== FALSE) {
$equalto = str_replace('DATE_UNIX', REQUEST_TIME, $equalto);
}

I hope that helps someone else. And that maybe one day the original coder can make these fixes to the module.

geek-merlin’s picture

Amongst other things fixed in #2124731: Replacement Pattern bugs
Please go there, test and set rtbc!

mlecha’s picture

This is a great module. I have the same date comparison issue. How can I help test?

I'm using Views Conditional release 7.x-1.1. I don't see a development release?

Thanks!

ofry’s picture

Is it fixed in 7.x-1.2 release? Check it, please. If not, please just reopen it.

ofry’s picture

Status: Active » Postponed (maintainer needs more info)
AlfTheCat’s picture

Version: 7.x-1.1 » 7.x-1.3
Status: Postponed (maintainer needs more info) » Active

This is still not working for me using the 1.3 version

I didn't try the patches of #2124731: Replacement Pattern bugs as that is marked duplicated of #2036829: Allow replacement variables in fields "This value", "Then" and "Or" which is marked fixed and committed to the 1.2 branch.

When I set an UNIX_DATE is less than [(excluded from display) datefield] show: [field x] else [sometext], "field x" always shows instead of the "sometext" if the "datefield" date has passed.

Geijutsuka’s picture

The comparison to DATE_UNIX and DATE_STAMP *does* seem to work, but only if the format of the compared date matches that of STAMP or UNIX replacement variable.

In other words...
By default, the DATE_STAMP replacement variable puts out this format: Thu, 02/19/2015 - 23:45 (which is the Date module's default "medium" format). If the date field you are comparing does not match this format, the logic will not work and you will get the first output you specify.
The conditional also works if you compare a unix timestamp field to DATE_UNIX (it's just a flat number, so it's easy to compare).

In short, try using the same format when comparing the two dates.

I've attached my sandbox view with three displays: one with a simple date field, one with a ISO date field, and one with a unix timestamp field if you want to take a look at my working view.

EDIT: the DATE_STAMP takes whatever format is set to Medium.

Geijutsuka’s picture

Upon further testing with setting different date/time formats in the Medium format, I get inaccurate results when I change from the 24-hour time to 12-hour time. So, for example, if have a date field set to Thu, 02/19/2015 - 11:45pm and the current time is Thu, 02/19/2015 - 4:59pm, for whatever reason the conditional sees 4:59pm as later than 11:45pm...

AlfTheCat’s picture

That's funny, I have a similar thing in the same view with the date comparisons, but with another field. I'm using node_registration and I'm counting users that are registered for a node. It counts double digit values as less than single digit ones. In a conditional field I'm saying: if the number of registrants is < 3, display " text A" if not, display nothing. This goes well up until the number 9. When I reach 10, it displays "text A" again.

In your case, it also seems to just compare the first hour digit. So the "1" from "11.45" is seen as less than the "4" from "4.59"
Again, this is with views aggregation turned on, not sure if that matters.

geek-merlin’s picture

some guidance: we'll have to replace format_date with something something that spits out iso format:
https://stackoverflow.com/questions/5322285/how-do-i-convert-datetime-to...

rogerpfaff’s picture

Is there anything happening regarding this topic? I need to compare two date fields. Would be great if I don't need to hack the module.

kalistos’s picture

I reached my goal with dates comparison by creating patch for date module.
https://www.drupal.org/node/1806856#comment-10369151

We mast to have similar date formats for comparison.

But by this module we have this formats: DATE_UNIX and DATE_STAMP.
By "Date" module we have this formats: Date, Date (ISO Format), Date (Unix Timestamp) (https://www.drupal.org/node/1455576).

So we have only this combination for comparison DATE_UNIX and Date (Unix Timestamp).

If we used another date field we should change date format...
BUT 1: with "Date and Time" field format we can change date format, but we get rendered field for comparison, with HTML and another possibles symbols (depends on theme overriding) what's wrong.
BUT 2: with "Plain" field format we can't change date format, because it's not working (this issue https://www.drupal.org/node/1806856 and this https://www.drupal.org/node/1763788).

So I created this patch https://www.drupal.org/node/1806856#comment-10369151 and used "Plain" field format for changed date format to custom Timestamp (created here admin/config/regional/date-time with "U" format).

Now it works. =)

ron_s’s picture

Use the Views Date Format SQL module to format the date on the database server, prior to rendering by Views. This will allow for comparisons in Views Conditional.

https://www.drupal.org/project/views_date_format_sql

rajmataj’s picture

I found a simple way to implement:

Modules

  • views 7.x-3.13
  • date 7.x-2.9
  • views_conditional 7.x-1.3

Steps:

  1. Go to your Date and Time's 'formats' tab: admin/config/regional/date-time/formats
  2. Add a timestamp format of: U
  3. Go to your Date and Time's 'types' tab: admin/config/regional/date-time and add a named type of 'timestamp', referring to the U format you just made
  4. Go to your View, alter your date field's setting of Choose how users view dates and times to timestamp
  5. In the conditional field, set the This value to be: DATE_UNIX - which uses the timestamp format as well

Your two conditions can now be compared.

imkartik’s picture

Thanks @rajmataj. Works like a charm.

vrwired’s picture

Re: #20, for full date range comparison example, also see https://www.drupal.org/node/2459061#comment-12046509

blueblot’s picture

Thanks @rajmataj. Just what I needed.

ianniscamillieri’s picture

Thanks so much #20!!!!

drupalsat’s picture

Thanks @rajmataj #20!!

Hejar’s picture

I was about to give up, thanks, rajmata. you can also skip step 1-3, just in your views use a custom date format and put "U".

stevechai’s picture

Hi,

How can i offset the time? Example for nodes created one day ago output A, for nodes created within today output B?

Thanks