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
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
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
Under the Advanced Section of the view, under the Other section, open Theme Information
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
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
This will output something like Sunday, December 1, 2013 - 12:00 - Sunday, December 1, 2013 - 12:00, based on the format you have chosen.
[field_my_time] - [field_my_time_1]
.
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);
}
}
}
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.