10

When using views to display start and end date by default it adds "to" text in between two dates, I was wondering what is the appropriate way to convert "to" to "-" or anything else.

Thanks

  • 1
    Same kind of question - drupal.stackexchange.com/questions/79311/… – RajeevK Sep 27 '13 at 4:28
  • When one will have answers, other should get closed as a duplicate. Good catch, @RajeevK – Mołot Sep 27 '13 at 7:12
  • @Mołot - That's why I didn't flag as duplicate. Hope somebody has something towards it & modified views data. I have done in D6 but not this one but very different thing. – RajeevK Sep 27 '13 at 7:43
9

My answers assume I have a field called "field_event_date" which shows as Content: Event Date in my view.

Answer #1 - Use View Template

  1. Under the Advanced Section of the view, under the Other section, open Theme Information

  2. Copy the filename of the last theme option for Field Content: Event Date, in my case it will be views-view-field--VIEWMACHINENAME--BLOCKMACHINENAME--field-event-date.tpl.php

  3. Copy the following into it:

    <?php
    
    /**
     * @file
     * This template is used to print a single field in a view.
     *
     * It is not actually used in default Views, as this is registered as a theme
     * function which has better performance. For single overrides, the template is
     * perfectly okay.
     *
     * Variables available:
     * - $view: The view object
     * - $field: The field handler object that can process the input
     * - $row: The raw SQL result that can be used
     * - $output: The processed output that will normally be used.
     *
     * When fetching output from the $row, this construct should be used:
     * $data = $row->{$field->field_alias}
     *
     * The above will guarantee that you'll always get the correct data,
     * regardless of any changes in the aliasing that might happen if
     * the view is modified.
     */
    ?>
    
    <?php print date("Y-m-d H:i:s", strtotime($row->field_field_event_date[0]["raw"]["value"])); ?>
     - 
    <?php print date("Y-m-d H:i:s", strtotime($row->field_field_event_date[0]["raw"]["value2"])); ?>
    

This will output something like 2014-08-09 20:15:00 - 2014-08-12 20:15:00, format using the date() format string.

Answer #2 - Use View Fields

  1. Under Fields, add Content: Event Date and set the Display to "Start Date Only"
  2. Under Fields, add Global: Custom Text and set Text to -
  3. Under Fields, add Content: Event Date and set the Display to "End Date Only"
  4. Under Field Settings, Set the three fields above as Inline.

This will output something like Sunday, December 1, 2013 - 12:00 - Sunday, December 1, 2013 - 12:00, based on the format you have chosen.

  • Answer #2b: do the same but exclude the start date from the display in the first field; don't add Global: Custom Text; and rewrite the results of the end date field with a replacement patterns to something like [field_my_time] - [field_my_time_1]. – Hugo Jan 14 '16 at 10:25
4

Although the answer above is correct, it is best practice not to use php in a TPL like this. Drupal comes with a bunch of fancy API's, one of the is the template_preprocess_views_view_fields (As seen here)

In your template.php do this:

function YOURTHEME_preprocess_views_view_fields(&$vars) {
 if($vars['view']->name == 'YOUR_VIEW') {
  if (strpos($vars['fields']['YOUR_FIELD']->content,'to') !== false) {
    $vars['fields']['YOUR_FIELD']->content = str_replace('to','-',$vars['fields']['YOUR_FIELD']->content);
  }
 }
}
2

In Date 7.x-2.8 and higher, the entire "start date to end date" string is translatable. This means that an easy way to do change the "to" to a "-" is to use the String Overrides module. Simply download and enable the module, then visit /admin/config/regional/stringoverrides. On that page, add "!start-date to !end-date" (without quotes) under "Original" and "!start-date - !end-date" (without quotes) under "Replacement." Click save. You may also need to flush you site's caches before seeing the change.

One benefit to this method is that it will change the "to" to a "-" on nodes and views at once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.