Hi there,

I have a problem to get the date popup module working the way I need. The popup works in every field, which is no exposed filter. But now I have a view with an exposed filter. I selected the option "popup" in the exposed filter options. The popup-field is working, but with the wrong format. I need the format "d.m.Y", but the displayed format is "y-m-d". What have I done wrong? Is there any option I haven't seen? Or is it a problem in the views-module? In that case I beg your pardon and create a new issue ;)

Thanks so far,

Tharya

Comments

That's true! I did not even notice when a client pointed this small but annoying issue. I found a possible solution here, I will let you know if this fixes it (in the short term). It would be great if we could sort out this. GREAT GREAT module by the way.

http://clipmarks.com/clipmark/DF28954C-0291-4C88-ABF9-F9C647171F4C

The link above actually did work - it's a hack though :(

I am also having this issue with Date 6.x-2.x-dev and views 6.x-2.10. I really do not want to hack the module as it will make updates more of a pain. Is there anyway to override this correctly?

also interested in doing this a clean way!

What we ended up doing was creating a custom module that did a hook_form_alter on the exposed views form filter. Below is an example of the code we used in our .module file. We were doing a filter by date range thus the code is written for both the min and max field filters.

<?php
/**
*  Implementation of hook_form_alter().
*/ 
function mymodule_form_views_exposed_form_alter(&$form, &$form_state) {
  if(
$form['#id'] == 'your-exposed-formid') {
   
$form['date_filter_1']['min']['#date_format'] = 'm-d-Y';
   
$form['date_filter_1']['max']['#date_format'] = 'm-d-Y';
}
}
?>

subscribing

Title:Date format in exposed filter (views)[resolved] Date format in exposed filter (views)
Version:6.x-2.2» 6.x-2.6

#5 works!

It actually fixes another problem - date field is now populated with proper relative values. have no idea why, maybe my defaults for field and for views were off. Thanks aelling!

Title:[resolved] Date format in exposed filter (views)Date format in exposed filter (views)

ok, maybe not that loud, sorry

I have a problem which isnt quite this one, but is closely enough related as to draw attention from the same users, I'd guess.

my exposed filter is generating args like ?q=Cal&date_filter[value][date]=2011-02 ; which are not getting interpreted by drupal as arguments that can be used by my views.. if i construct my own args (?q=Cal/2011-02) it works fine.

Any support is appreciated. Ive been banging my head for a while.

Thanks!
Mark

I am trying to accomplish the same thing, but I can't get #5 to work, and I have really no idea why ;-(

This is my code:

function mymodule_form_views_exposed_form_alter(&$form, &$form_state) {
  if($form['#id'] == 'views-exposed-form-events-all-search-panel-pane-1') {
    $form['date_filter_1']['min']['#date_format'] = 'm-d-Y';
    $form['date_filter_1']['max']['#date_format'] = 'm-d-Y';
  }
}

The if condition returns true, so the id is correct, and it has to be related to what happens next..

Could it be because it is built within a panel? I should also note that I am using popup calender in these range fields.

Any suggestions? Thanks

Max

Maxim75

I have placed code from above in general form_alter handler (i.e. in mymodule_form_alter) and everything works fine. Code looks like

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id){
...
  if(
$form_id == 'views_exposed_form'){
    if(
$form['#id'] == 'views-exposed-form-events-all-search-panel-pane-1'){
     
$format = 'm-d-Y';
     
$form['date_filter']['min']['#date_format'] = $format;
     
$form['date_filter']['max']['#date_format'] = $format;
    }
...
}

?>

Beside of this, are you sure that your exposed date filter has identifier 'date_filter_1' ? You can watch the identifier of your filter by clicking on filter in view configuration page. There should be Filter identifier input - copy the value of filter identifier from that input

Cheers.

Thanks a lot, Ruslan!

I got it working. you were right, my bad...
I used the native id where in an ancient past I had configured it with an overriding id, with this all is well ;-)

Cheers!

Max

Status:Active» Fixed

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

I am encountering this same problem in Drupal 7. I cannot configure the date format in the exposed filter with Popup calendar. It only goes by YYYY-MM-DD where the date field I created goes format like Jun 22 2011.
any solution without having to use form_hook_alter?

Thanks

Version:6.x-2.6» 6.x-2.x-dev
Assigned:Unassigned» enboig

I have created two form_alter's to achive this. Now the date format in views filter is configurable. I hope this functions could enter date_popup module....

<?php
function date_popup_form_views_ui_config_item_form_alter(&$form, $form_state) {
  if (
'2' != substr(views_api_version(), 0, 1)) {
   
// Only continue for Views 2.x
   
return;
  }
  if (!empty(
$form['options']['expose'])) {
    if (isset(
$form['options']['value']['type']['#options']) && array_key_exists('date', $form['options']['value']['type']['#options'])) {
      
$form['options']['value']['type']['#options']['user_defined_date'] = t('A user defined date using PHP date() wildcards');
      
$form['options']['value']['date_format'] = array(
        
'#type' => 'textfield',
        
'#title' => t('Date_format'),
        
'#default_value' => !empty($form_state['handler']->options['value']['date_format']) ? $form_state['handler']->options['value']['date_format'] : 'Y/m/d',
       );
    }
  }
}
function
date_popup_form_views_exposed_form_alter(&$form, $form_state) {
  foreach (
$form_state['view']->filter as $field => $filter) {
    if (
$filter->options['value']['type']=='user_defined_date') {
      switch(
$filter->options['operator']) {
        case
'between':
         
$form[$filter->options['id']]['min']['#type'] = 'date_popup';
         
$form[$filter->options['id']]['min']['#size'] = '20'; //YYYY-MM-DD HH:MM:SS
         
$form[$filter->options['id']]['min']['#date_format'] = $filter->options['value']['date_format'];
         
$form[$filter->options['id']]['max']['#type'] = 'date_popup';
         
$form[$filter->options['id']]['max']['#size'] = '20'; //YYYY-MM-DD HH:MM:SS
         
$form[$filter->options['id']]['max']['#date_format'] = $filter->options['value']['date_format'];
          break;
        default:
         
$form[$filter->options['id']]['#type'] = 'date_popup';
         
$form[$filter->options['id']]['#size'] = '20'; //YYYY-MM-DD HH:MM:SS
         
$form[$filter->options['id']]['#date_format'] = $filter->options['value']['date_format'];
          break;
      }
    }
  }
}
?>

I've updated the snippet above for views 3.x

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id){
...
  if(
$form_id == 'views_exposed_form'){
    if(
$form['#id'] == 'views-exposed-form-events-all-search-panel-pane-1'){
     
$format = 'm-d-Y';
     
$form['date_filter']['value']['#date_format'] = $format;
    }
...
}
?>

subscribe

enboig, thank for your code.
it helped me with some revisions:

<?php
function MYMODULE_form_views_ui_config_item_form_alter(&$form, $form_state) {
  if (
'2' != substr(views_api_version(), 0, 1)) {
   
// Only continue for Views 2.x
   
return;
  }
  if (!empty(
$form['options']['expose'])) {
    
$form['options']['value']['type']['#options']['user_defined_date'] = t('A user defined date using PHP date() wildcards');
    
$form['options']['value']['date_format'] = array(
      
'#type' => 'textfield',
      
'#title' => t('Date_format'),
      
'#default_value' => !empty($form_state['handler']->options['value']['date_format']) ? $form_state['handler']->options['value']['date_format'] : 'Y/m/d',
     );
  }
}
function
MYMODULE_form_views_exposed_form_alter(&$form, $form_state) {
  foreach (
$form_state['view']->filter as $field => $filter) {
    if (
$filter->options['value']['date_format']) {
     
$el_id = $filter->options['id'];
      if (
$filter->options['expose']['identifier']) {
       
$el_id = $filter->options['expose']['identifier'];
      }
      switch(
$filter->options['operator']) {
        case
'between':
         
$form[$el_id]['min']['#date_format'] = $filter->options['value']['date_format'];
         
$form[$el_id]['max']['#date_format'] = $filter->options['value']['date_format'];
          break;
        default:
         
$form[$el_id]['#date_format'] = $filter->options['value']['date_format'];
          break;
      }
    }
  }
}
?>

I believe #17 is exactly what I need but I can't figure where I need to insert similar code to make it work.

My filter date_filter only accepts 'Y-m' and I need it to accept a 'F Y' format.

Do I have to create a custom module to do that?

This is the last hurdle of my first Drupal7 website!

Insert the code in a custom module.
For example: create a folder "mymodule" in sites/all/modules and add two files:

  1. mymodule.info which contains the following:
    ; $Id: mymodule,v 6.x-dev 2011/10/20 16:45:57 tomsm $
    name = My module 2
    description = My custom module to alter the date format in views filters
    package = Custom modules
    core = "6.x"
    version = "6.x-dev"
  2. mymodule.module which contains the code you need to insert:
    <?php
    // $Id$
    /**
    * @file
    * This is my custom module to change to date format to d-m-Y in exposed views filters
    */
    function mymodule_form_alter(&$form, &$form_state, $form_id){
      if($form_id == 'views_exposed_form'){
        if($form['#id'] == 'views-exposed-form-order-panel-page'){
          $format = 'd-m-Y';
          $form['date_filter']['value']['#date_format'] = $format;
    }
    }
    }

Replace 'views-exposed-form-order-panel-page' with the id of your view. You can use firebug to determine what that is.
Activate your custom module and clear all caches and the date format should change.

Thank you tomsm very much for your input.

I did what you suggested but it doesn't work on my dev site. EDIT: After a little more fiddling, it does work - see below
I made sure everything is setup fine in the 5-line function but my filter will only accept 'Y-m' and not 'F Y'.

Now, maybe this is where I got it all wrong. My filter is not displaying any values, it's just a text input box in which users type a date like 'May 2005', 'May' or '2005'. So maybe I'm not seeing the result of my custom module.

I think maybe I got confused and what I want is not to display a date in a different format but for the filter to accept a different format. How should I go about that?

Thanks to anyone who will help me being a better hmm drupalist

N

EDIT: I forgot to enable the Date Views module which is why it wouldn't work. All is well now!

Subscribe - Thank you very much guys solution 16 was the easy fix.

Version:6.x-2.x-dev» 7.x-2.x-dev
Status:Closed (fixed)» Active

Hi,

I can't fix this using D7, views 3 and Date 7.x-2.x-dev version.
When I expose my date filters, they still are in 'Y-m-d' format.
There is any way to do this in D7?
The solution in #21 didn't helped me, here is my code :
module : date_fixed_format

<?php
function date_fixed_format_form_alter(&$form, &$form_state, $form_id){
    if(
$form_id == 'views_exposed_form'){
        if (
$form['#id'] == 'views-exposed-form-liste-des-offres-page-1') {
           
$form['date_filter']['value']['#date_format'] = 'd-m-Y';
        }
    }
}
?>

Someone can say me how to format my exposed filters with date popup ?

Thanks

Version:7.x-2.x-dev» 6.x-2.x-dev
Status:Active» Closed (fixed)

Please please please don't reopen closed issues for one version and switch them to another version. The code in D6 and D7 is totally different. Very few D6 solutions will work in D7. Very few problems are the same in both versions, even if the symptoms look the same. If you think you have a problem that doesn't already have a D7 issue, open a new issue for that, don't hyjack a D6 issue.

Ok, sorry ... I apologize.
I'm still not familiar with the issue system ...
I'll open a new issue for D7.

Was a D7 issue ever opened for this? I am looking to do the same thing and cannot find a D7 version of this issue.

Same here, would love to have something that works since Date Browser is still useless

subscribe

For those looking for the 7.x solution, thread is here http://drupal.org/node/1409120

Hi. If anyone is interested, I have a snippet that alters the format used for a pair (start - end) of exposed date filters (views 3.x) and allows flexible input. This is done by setting the filter property of the form element and by adding an element validate function that interprets the input before the main validation routines check it.

Fisrt the form alter bit:

function mymodule_form_views_exposed_form_alter(&$form, &$form_state) {
  // Alter the date format value
  $format = variable_get('mydatefield_filter_format', 'j/m/Y H:i');
  $form['field_mydatefield_value']['min']['#date_format'] = $format;
  $form['field_mydatefield_value']['max']['#date_format'] = $format;
  // Add our validation function to each of the date filter elements, making sure it is first validation function
  if (isset($form['field_mydatefield_value']['min']['#element_validate'])) {
    array_unshift($form['field_mydatefield_value']['min']['#element_validate'], 'mymodule_views_exposed_date_filter_validate');
  }
  else{
    $form['field_mydatefield_value']['min']['#element_validate'] = array('mymodules_views_exposed_date_filter_validate');
  }
  if (isset($form['field_mydatefield_value']['max']['#element_validate'])) {
    array_unshift($form['field_mydatefield_value']['max']['#element_validate'], 'mymodule_views_exposed_date_filter_validate');
  }
  else{
    $form['field_mydatefield_value']['max']['#element_validate'] = array('mymodule_views_exposed_date_filter_validate');
  }
}

Note the use of array_unshift to get my validation function to the start of the list if an element validation function is already specified.

Next the validation function:

function mymodule_views_exposed_date_filter_validate(&$element, &$form_state) {
  // If a date value has been supplied, then massage it to be acceptable if possible
  $minmax = (substr($element['#id'], -3) == 'min') ? 'min' : 'max';
  $format = $element['#date_format'];
  // Check whether a date value has been specified in either min or max fields
  if (!empty($form_state['input']['field_mydatefield_value']['min']['date']) ||
    !empty($form_state['input']['field_mydatefield_value']['max']['date'])) {
    // Yes - so make sure there is a date values in this field
    if (empty($element['#value']['date'])) {
      $date = ($minmax == 'min') ? 'today' : 'now';
    }
    else {
      $date =  $element['#value']['date'];
    }
    $element['#value']['date'] = date($format, strtotime($date));
    $element['date']['#value'] = date($format, strtotime($date));
    if (isset($form_state['input']['field_mydatefield_value'][$minmax]['date'])) {
      $form_state['input']['field_mydatefield_value'][$minmax]['date'] = date($format, strtotime($date));
    }
    if (isset($form_state['values']['field_mydatefield_value'][$minmax]['date'])) {
      $form_state['values']['field_mydatefield_value'][$minmax]['date'] = date($format, strtotime($date));
    }
  }
}

The supplied input is processed through the function "date($format, strtotime($date))" which will make a quite good interpretation of any supplied input as a date/time value and re-format into the represetnation expected by the date module validation. It can interpret strings like "today", "now", and just time strings are expanded to include today's date.

This validation function will ensure that if only one of the start or end dates is supplied, the other (either "today" for the start, or "now" for the end) will be added, allowing for easier entry of time ranges for the current date.

Note that user supplied input is located in two different places in the $element array, and is in both the $form_sate['values'] and $form_state['input'] arrays (yuk!!!). All four places should be updated the same way.

Note the use of the last three characters of the element id to determine whether this is for the start or end date.

The date validation function will replace the "$form_state['values']['field_mydatefield_value'][$minmax]['date']" item with a "$form_state['values']['field_mydatefield_value'][$minmax]" value when it has validated the input, so the function only updates the original value if it exists. See below for a dump when the start has been validated, but not the end, date which has had the string "now" translated to an actual date/time.

    [field_mydatefield_value] => Array
        (
            [min] => 2014-02-22 10:30
            [max] => Array
                (
                    [date] => 2014-02-22 13:42
                )
        )

Hope this helps someone.