Date formats

One of the greatest strengths of Drupal is its modular design and the ability to customize the way your content is structured. Being able to create custom content types and fully control display rules on how your data are presented is one example of this flexibility. Another example is the ability to display image files stored in the system in a variety of sizes and crops, to fit the needs of every layout. Still another is the ability to fully customize the way dates and times are displayed. This is done through creating and managing date formats, which is the topic of this article.

There are many ways to present a date. On the one hand, there is granularity.How specific do you want to be? Just the year? Year, month, and day? All of the above plus the day of the week? Or go as precise as seconds?

Besides the granularity, there are issues of presenting each piece of the date and time information. 24 hour clock or 12? Leading zeros on hours or none (i.e 9:00 or 09:00)? Day of the week written in full or in a three letter abbreviation? Do we want a slash or a dash between month and day? (i.e 12/24 or 12-24 for Christmas Eve).

Finally, there is the issue of order - do we want month followed by day (standard American practice) or day followed by month (standard European practice, don't ask me which one makes more sense).

All these issues - granularity, presentation, and order - can be customized in Drupal 7.

How do I create a custom date format?

Let's say you want to create a date format for your business plan that would show only the month and the year, so that you could have a calendar of events that would have lines like this:

Begin initial testing - March 2012
Re-design based on testing - May 2012
Launch to limited audiences - June 2012
Full scale launch - July 2012

What do you need to do? You need to go over to Administration > Configuration > Date and time (path: yoursite/admin/config/regional/date-time )

The system will show you what date types and date formats you already have. By default, you get three date types - long, medium and short and 14 different date formats. You might be wondering, why do we need date types and date formats? We need these two things to achieve maximum flexibility. Date formats are a set of rules of what gets displayed in a date that have to deal with the three parameters mentioned earlier - granularity, presentation, and order. This is where the actual customization happens. Date types are simply labels to attach to date formats. They are arbitrary and can be changed and re-assigned at any point.

You can create new formats and new types. To create a new format, click on the "Formats" tab (path: yoursite/admin/config/regional/date-time/formats ) and then click "Add format" (path: yoursite/admin/config/regional/date-time/formats/add ).

You are greeted by a single input field and a reference to the PHP manual. It looks scary but it isn't. Instead of asking for "show the month as a word and then a four digit number to show the year" to get "March 2011" you use PHP shorthand and ask for "F Y".

Once you create your custom format, it is added to the set of your formats - you should now have 15 instead of 14. You can now re-assign an existing type to your new format - for example, you may decide that this new format should be matched to the "short" type. Or you may create a brand new type for this new format - and call it something like "Month year".

The new custom types are added to your options when you create a date field in a content type, or when you decide how you want to represent dates in your Views.

In sum, custom date formats allow you to decide how you want to display dates and times; and being able to assign different date types to different fields and Views mean that you are not stuck just with one format for everything, but can output dates and times differently for each area of the site.