3

I have a content type with a start date, and an option for an end date. When this field is displayed in a view, I would like to see

1 May 2012
2-3 May 2012 

(etc...)

At the moment, I can only adjust the format to display

2 May 2012 - 3 May 2012

I've also learnt that even though an end date may not be provided, it defaults to the start date, so I can't use any rewrite rules in Views module to get around this.

Is there anyway to adjust the Date format when using a start and end date?

  • Sorry to ask, but your example if confusing. You are saying you would like to see this: 1 May 2012 2 - 3 May 2012, but are currently seeing this: 2 May 2012 - 3 May 2012. What is the difference except there is an extra 2 in the first example which I am assuming is a typo? – 9ee1 Feb 13 '13 at 2:35
  • If I have a start date of 2nd May 2012, and end date of 3rd May 2012, then I would like to show 2-3 May 2012 in my view... as it doesn't look right to display "2 May 2012 - 3 May 2012".. – user5681 Feb 13 '13 at 2:46
  • 1
    I doubt there's a way without using PHP, unless you can find a custom module that has that very specific piece of functionality. Why can't you use PHP? – Clive Feb 13 '13 at 10:10
  • So far, my team has developed a number of websites with zero custom PHP code. This makes things easier to maintain, and ensures requirements are met quickly. I was hoping I didn't have to break this mandate. – user5681 Feb 18 '13 at 2:10
1

You can coax something out of Views without PHP (see Partial Solution below) but not, I think, quite what you want. A module or - against your team policy Views PHP - may be required.

Discussion

Part of why some of the usual Views workarounds won't solve this problem is the edge cases. If we knew that ranges of dates would always be within the very same month, then this could be done in Views without PHP using the approach outlined below. But that seems unlikely! You need to consider what you want to do when you get cases such as 31 May 2013 to 2 Jun 2013 or 28 Dec 2012 to 4 Jan 2013. What do you want displayed? If you want '31 May - 2 Jun 2013' and '28 Dec 2012 - 4 Jan 2013' respectively, but in non month-crossing cases '2 - 3 May 2012', then some code is going to need to be involved somewhere.

Partial Solution

A partial solution can be massaged out of Views. It will avoid repeating the year but not the month. So you get, e.g. '2 May to 3 May 2012', '28 Dec to 4 Jan 2013', '31 May to 2 Jun 2013' from it. Just possibly this might be close enough for some use cases.

The strategy is to add the date field twice (excluding it from display) and apply different formats / configurations. You can then use Global: Custom text fields to join the results together.

  1. Create two date formatters. One to give '12 Mar' when given 12th March 2012 (j M) and one to give '2012' when give 12th March 2012 (Y).

  2. Add the date field (and exclude from display). Set it to show both the START and END dates using the '12 Mar' format. When both START and END are present this will yield '3 Mar to 6 Mar' etc. When only START is present, it will yield '3 Mar'. Recommendation use the 'Administrative title' in the 'More' section to document this, e.g. 'Date or range without year'. This helps at the final step.

  3. Add the date field (and exclude from display). Set it to show just the END date. Use the '2013' format. When an END date isn't present, Views will fall back to using the start date. Recommendation use the 'Administrative title' in the 'More' section to document this, e.g. 'Year'. This helps at the final step.

  4. Add a Global: Custom text field and use 'Replacement Patterns' to output the 'Date or range without year' followed by the 'Year'. (Easy if you've documented with the 'Administrative title' - trial and error if not.)

Note: If you want to detect the absence of an end date at step (3), Views will do this if you make use of the No Results area. It only defaults to the Start Date if the No Results area is not used. That gives some, albeit, limited scope to vary formatting depending on the presence of absence of an end date.

  • Thanks. I tried a similar approach. I've submitted a patch to replace the separator, then I'll try to submit a Date Separator module to handle these sort of edge cases. I've used your suggestion for the time being. – user5681 Mar 10 '13 at 1:36
  • Would this not fail if the start date would be in one year and the end in the next one? – Christoph Oct 9 '14 at 14:02

Your Answer

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