If I set up a CCK date field to have a To date, it displays like this:
24 July, 2009 - 30 July, 2009

Is there a way to get it to intelligently fold the duplicated parts, so it says instead:

24 - 30 July, 2009

Is this currently possible?
I found this in the queue from 5.x: #170351: Simpler date range display but it only handles time it seems.

CommentFileSizeAuthor
#7 date_folding_formatter.zip4.35 KBjoachim

Comments

danielb’s picture

Yes it is possible to do something like this with PHP. I've done it on this site (click links in the table - notice how the date display on the page differs for one-day events vs. events over several days). I don't have a backup of the code I wrote for the site, and I don't have access to the site's FTP anymore.
My algorithm basically compared the two dates at each level of granularity (year, year+month, year+month+day) and that would decide whether to print out a particular section of the first date (print it if it differs). If any parts of the first date were to be printed, then the hyphen was appended to the first date as well. You could do the same with the comma between month and year.

Then you have to consider how to do times of day as well, and perhaps writing out a few examples of what it should look like would help.

joachim’s picture

Given that dates at some point end up as strings of the form YYYMMDDTHHMMSS, I wonder if we could just run the two date strings through substr_compare(), with successive offsets, until they fail to match.
Then it's just a matter of changing the granularity on the first date as we format it: the more they matched, the more granularity we remove.

karens’s picture

Status: Active » Closed (works as designed)

This is very very complicated because it has to work in any language and for any date format. The folding currently being done is hard enough and there were lots of issues getting it working, I'm not anxious to push it any further.

The way to do things like this is to override the theme and do it yourself. You don't have to make it work in every possible situation, only your own, so that's easier.

joachim’s picture

Ok thanks Karen.
Maybe we can start a page in this module's handbook for snippets for this.

matt.nz’s picture

I'm looking for a better way to override date folding.

At the moment I'm editing the date.theme file to override the date range display function with strtotime. This is less than ideal as the input dates don't contain a year. If I include a year, I need to include it in the single dates as well. But if I then override the display for single dates, it changes every single date on the site. It all feels very hack-ish, and there must be a better way to do it.

Any suggestions appreciated!

danepowell’s picture

I'm surprised this hasn't been replied to in two years, especially now that D7 has come out with such a focus on improved UX. Having to use date range formats like "March 12, 2012 - March 14, 2012" vs "March 12-14, 2012" is a huge detractor from usability. Is there any chance that this could be reconsidered for implementation in Date core? If not, does anyone have solid examples of how to accomplish this in the theme layer? I'm using Date 7.x-2.x

joachim’s picture

StatusFileSize
new4.35 KB

I actually wrote this up as a module for D6, but didn't release it given Karen's comments about problems making something like this generic enough to work with different formats and translations.

Here's the code.

danepowell’s picture

Awesome, I will have to try that out- thanks for posting!