Date and Calendar Module in Drupal 5: Part 1

Recipe 33: Understanding Date formats

Drupal dates are typically stored in one of two ways. Core Drupal dates—including Node: Created Time, and Node: Updated Time—are stored as Unix timestamps. Contributed module date fields can be stored as either a timestamp or a format known as ISO. Neither style is particularly friendly to human readers, so both field types are usually formatted before users see them. This recipe offers a tour of places in Drupal where dates can be formatted and information on how to customize the formats.

What's that Lucky Day?
The Unix timestamp 1234567890 fell on Friday the 13th, in February, 2009. This timestamp marks 1,234,567,890 seconds since January 1, 1970. The same date/time combination would be stored in a date field in ISO format as 2009-02-13T23:31:30+00:0. ISO is an abbreviation for the International Organization for Standardization

  1. Opening the browser windows side-by-side will help you understand date formatting. In the left window, open date-time to view the settings page for date and time. In the right window, open the API page of code that defines these system date time settings at Compare each item in the $datemedium array, for instance, with the associated Medium date format drop-down.
    • a – am/pm
    • D – Day, Mon through Sun
    • d – Date, 01 to 31 (with leading zeroes)
    • F – Month, January through December (mnemonic, F = Full name)
    • g – Hours, 1 through 12
    • H – Hours, 00 through 23
    • i – Minutes, 00 to 59
    • j – Date, 1 to 31 (No leading zeroes)
    • l – Sunday through Saturday
    • m – Month, 01 through 12
    • M – Month, Jan through Dec
    • s – Seconds, 00 through 59 (with leading zeroes)
    • S – Month Suffix, st, nd, rd, or th. Works well with j
    • Y – Year, Examples: 1999 or 2011
  2. Drupal 5 Views Recipes

    Below is the list of codes for many commonly used date and time formats. A more comprehensive list appears at

  1. Explore Drupal places where these codes may be used. The first four locations in the table below are available in the Drupal administrative interface. The last three involve editing files on the server—these edits are completely optional.



CCK field setup

Custom Input formats



Drupal 5 Views Recipes

After the field widget is specified


Near the top of the page.


Drupal 5 Views Recipes

Near the bottom of the page:


Drupal 5 Views Recipes

Formatting Fields in Views.


CCK Date fields are set via the Options drop-down in the Fields fieldset.


Drupal 5 Views Recipes

Custom date formats for core fields, such as Node: Created Time are set via handler and options from elements.


Drupal 5 Views Recipes

Default Date and Time settings


Set the default time zone, Short, Medium, and Long date formats, and the first day of the week.


Post Settings

This may be one of the harder-to-find settings in Drupal, enabling the Post settings to be turned-off for specified content types. (An example of a post setting would be: Submitted by admin on Sun, 10/12/2008 - 4:55pm.

The setting is found on the right-hand side of this URL: admin/build/themes/settings

Use the following mouse click trail to get to this URL:

Administer | Site Building | Themes | Configure

(Click on the Configure tab at the top of the page. If you click on the Configure link in the Operations column, you will still need to click the Configure tab at the top to get to the global settings.)


Drupal 5 Views Recipes

Variable overrides in settings.php

You may override variables at the bottom of the /sites/default/settings.php file. Remove the appropriate pound signs to enable the $conf array, and add a setting as shown below. Note that this is a quick way to modify the post settings format, which draws from the medium date variable.

$conf = array(

#   'site_name' => 'My Drupal site',

#   'theme_default' => 'minnelli',

#   'anonymous' => 'Visitor',

'date_format_medium' => 'l F d, Y'


  • *.tpl.php files



<?php print format_date($node->created, 'custom', 'F Y'); ?>

  • comment.tpl.php

<?php echo t('On ') . format_date($comment->timestamp,
  'custom'  , 'F jS, Y'); ?> <?php echo theme('username',
  $comment) . t(' says:'); ?>

  • template.php

Redefine $variables['submitted']

Example from blommor01 theme:

  $vars['submitted'] =  t('!user - <abbr class="created"
  title="!microdate">!date</abbr>', array(

   '!user' => theme('username', $vars['node']),

   '!date' => format_date($vars['node']->created),

   '!microdate' => format_date($vars['node']->
  created,'custom', "Y-m-dTH:i:sO")


Recipe notes

Note that when using the PHP date codes, additional characters may be added, including commas, spaces, and letters. In the template.php example, a backslash was used to show that the letter 'T' will be printed, rather than the formatted return values. Below are more examples of added characters:

F j, Y, g:i a // August 27, 2010, 5:16 pm
m.d.y // 08.27.10

You may occasionally find that an online date converter comes in handy.

Recipe 34: Block of upcoming workshops

This recipe is the foundation for many other recipes in this article.

The Node Go To module is a little-known treasure that makes it easy to enter content. This recipe also makes use of the now option, available in the Date module.

  1. Install and enable all of the modules in the ingredients
  2. Go to admin/settings/date-time and set the default time zone.
  3. Go to admin/content/types/add and create a content type named Workshop, of type workshop. As usual, remove the Body field in the Submission form fieldset. We'll create a workshop_descriptionfield instead
  4. Go to admin/content/types/workshop/add_field and add a field named workshop_date. We have several field types to choose from, however, I recommend the Datestamp format for greater flexibility. Drupal's format_date() function is used to create graphical date badges. This function requires a Unix timestamp as the first parameter (the Drupal Date module uses the term Datestamp for fields stored in Unix timestamp format). The Text Field with jquery pop-up calendar offers a friendly way of entering dates.
  5. Drupal 5 Views Recipes

  6. Press the Create field button. All the settings at admin/content/types/workshop/fields/field_workshop_date may be left as default, or edited, as per your preference. Click the Save field settings button.
  7. Go to admin/content/types/workshop/add_field, and add a text field named workshop_description. Click the Create field button.
  8. Set the number of rows to 4 and set the Text processing to Filtered text(user selects input format) at admin/content/types/workshop/fields/field_workshop_description. Click the Save field settings button.
  9. The Node Go To module redirects the browser to a specified URL after creating, updating, or deleting a node. Configure the module at admin/settings/nodegoto. Open the fieldset labeled Set the path to redirect for, when INSERTING. Enter node/add/workshop as the path as shown:
  10. Drupal 5 Views Recipes

  11. Create several workshops, at node/add/workshop. As a result of the configured Node Go To module, after each node is submitted, the entry form for another workshop appears.
  1. Create a new view, at admin/build/views.






Page and block of workshops

Provide Page View





View Type

Full Nodes


Upcoming Workshops

Use Pager


Nodes per Page


Provide Block


View Type

List View


Upcoming Workshops

Nodes Per Block


[More] Link?


Fields (use default handlers and options.)

Node: Title

Datestamp: workshop_date (field_workshop_date)


Node: Published - Equals Yes

Node: Type - Is One Of Workshop

Datestamp: workshop_date - Date (field_workshop_date)

Set the Operator to greater than or equal to.

Enter the word now in the Option field.

Thus we are filtering to workshops that will be held in the future.


workshop_date (field_workshop_date) - Ascending

  1. Save the view
  2. Go to admin/build/block and add the workshops block to the right sidebar. Enjoy the view.
  3. Drupal 5 Views Recipes

  1. We will add one more set of steps in preparation for Recipes 35 – 41. Clone this view six times at admin/build/views/workshops/clone.
    Rename each view as follows:





Exposed Filters






Browse Workshops






Views Popup


    For each view you may also uncheck the Provide Block View checkbox and, Save the view.

Recipe notes

  • The Submit More module offers similar functionality to Node Go To.
  • The GCal Events module offers a handy way of creating blocks of upcoming events. Events are drawn from Google Calendar.
  • If you are still using Date 1.x, be sure to install the JavaScript Tools module and enable the JSTools Calendar module. This will give you a pop-up calendar selection tool similar to the one available in Date 2.x. You may also implement Recipe 44 to upgrade the Date module.

Recipe 35: Exposed Filters

Completed Recipe 34, exposed_filters view

We finally turn to the Exposed Filters fieldset, which exposes one or more selection lists to users, enabling them to choose how to filter the view (until now, all of our filters have been applied by the web site administrator, rather than by end users). In this recipe, we enable users to filter by the date field. We will explore the Views Date Range Filter module which offers additional options, in the recipe that follows.

  1. Edit the exposed_filters view, at admin/build/views/exposed_filters/edit.
  2. Go to the Filters fieldset and click on the Expose button for the Datestamp: workshop_date field.

    The delete icon disappears from the Ops column for that row. However, the field now appears in the Exposed Filters fieldset. If you want to delete a field that has been exposed, you must first delete it from the Exposed Filters, before it is available to delete in the Filters fieldset.

  3. Open the Exposed Filters fieldset. Review how it looks, but leave the default settings intact. (Note that there is no Add Filter drop-down available. Exposed Filters field are added and exposed via the Filters fieldset, as in step 2, above.)
  4. Save your view and go to A new selection list is available.
  5. Drupal 5 Views Recipes

Recipe notes

  • Exposed  Filters offer powerful search capabilities to end users.
  • Explore the date_views_filter_handler in to see the code behind date filters. The code is complex, as is typical of date functions, but well-commented, especially in the Date 5.x-2.x version.

Recipe 36: Views Date Range Filter module

  1. Install and enable the Views Date Range Filter module.
  2. Edit the exposed_filters view at admin/build/views/exposed_filters/edit. Go to the Exposed Filters fieldset and click on the delete icon to remove the Datestamp filter.
  3. Click on the Save and edit button (always a good idea after working with the Exposed Filters fieldset).
  4. Go to the Filters fieldset and delete the Datestamp filter there too.
  5. Add the Date Range Filter and press the Expose button.
  1. Save the view and test the various date range options. Compare with the date filter options from the previous recipe.

The selection list for the Views Date Range Filter

Drupal 5 Views Recipes

Compare with the drop-down list
from Recipe 35

Drupal 5 Views Recipes


    In addition to all the preset ranges (within the past week and in the next month), the Date Range module lets you choose events occurring between two dates (Custom    date range) It even allows you to exclude a range of dates (Outside date range) and to specify dates such as today, yesterday, today-7, today-2M,  or today-1Y.

Recipe notes

There are some other subtle differences to be found when filtering by date versus filtering by a Date Range. The Views Date Range Filter module allow placement of values such as today+1 or today-1Y in the Value text box of the Filters fieldset. By contrast, Date 2.x allows values such as now and now +1 day. Spacing and capitalization are important. Copy and paste from the helpful notes under the field, or from the README.txt file to ensure the appropriate format.

Recipe 37: Exposed Filter settings

Completed Recipes 34 and 35

The Exposed Filters fieldset provides four options, as shown:

Drupal 5 Views Recipes

The handbook page for filters ( offers helpful documentation on these options. After studying that page, the best way to get to understand these filter options is simply to try them. While testing the options keep an eye on both the Filters and Exposed Filters fieldsets (two of the options: Filter settings Default and Lock Operator correspond directly to settings in the Filters fieldset).

  1. Create a vocabulary for workshop topics at admin/content/taxonomy/add/vocabulary

Vocabulary name



Topics for workshops

Help Text

Select one or more topics






Multiple Select and Required.

  1. Add terms such as Nutrition, For Kids, and Gardening to the Topic vocabulary at admin/content/taxonomy.
  2. Edit the existing workshops nodes and assign appropriate terms. It may be helpful to use the view to identify the workshops. Open the exposed_filters view and instead of choosing Edit (which will edit the view), click on each title link. Now choose Edit to edit that particular node. For the sprouting workshop, assign to it the topics such as Nutrition and Gardening.
  3. Edit the view at exposed_filters/edit. Add the Taxonomy: Terms for Topic taxonomy to the Filters fieldset and click Expose.
  4. Experiment with the Exposed Filters options. Click the checkbox for each of the four Exposed Filters options, save the view, and examine the results. Your results may appear slightly different than the images below depending on whether your taxonomy is set to be required and depending on whether several options are checked at once.

Optional: Unchecked

Does not include an All selection, and no workshops are displayed, by default.


Drupal 5 Views Recipes

Optional Checked

Includes an All selection, and all workshops are

(The None option only appears if the taxonomy is
not required.)


Drupal 5 Views Recipes

Filter settings Default: Unchecked

All of the workshops are displayed.


Drupal 5 Views Recipes

Filter settings Default Checked

Only the videos are shown, by default, at the
 exposed_filters URL. The Video selection was
 set in the Filters fieldset.


Drupal 5 Views Recipes


Force Single: Unchecked


Drupal 5 Views Recipes

Force Single: Checked


Drupal 5 Views Recipes

Lock Operator: Unchecked


Drupal 5 Views Recipes

Lock Operator: Checked

The Is One Of operator from the Filters fieldset is
 locked in for use by Exposed Filters. Thus, the
option does not appear for end users.


Drupal 5 Views Recipes


Recipe notes

If you lock an operator, consider using the Field's Label to give some information on how to use the drop-down values.

Recipe 38: Summary Views

Completed Recipe 34, Summary View

Views offers a wonderful feature, called Summary Views, available in the Argument fieldset. It creates a hyperlinked list of topics with a count of items.

  1. Edit the summary view, created in Recipe 34.
  2. Add the Datestamp: Workshop Date argument, with the following settings:


Summary, sorted ascending


Workshops in %1


summarize by month




    Drupal 5 Views Recipes

  1. Enjoy the View.
  2. Drupal 5 Views Recipes

  3. Click on a month link. Notice that the month is included as an argument in the URL. The title of the page substitutes the month for the %1.
  4. Drupal 5 Views Recipes

  5. Try out the wildcard at All the workshops are displayed, using the pager count of 10.
  6. We would like the view to display up to 10 workshops per page but prefer to display the next 12 months in the Summary View—so we will add argument handling code to the view. Edit the view at summary/edit. In the Arguments fieldset open the Argument Handling Code and add the following:

    code 1::

    if (!$args[0]) {
    $limit = 12;

    Thus, if the argument in the workshop_summary view is not set (for instance, at the URL, as opposed to or, then the page limit will be 12 rather than the usual limit of 10 items that were set in the Page fieldset.

    You will now see all 12 months in the summary—if you actually have 12 months of upcoming workshops.

  7. (Steps 7–9 are optional.) If you would rather have the view summary display workshops by topic, edit the view at summary/edit.
  8. Delete the argument and the argument handling code. Replace it with Taxonomy: Term Name


Summary, sorted ascending


Workshops - %1


(leave blank, or set to 0)



    Drupal 5 Views Recipes

  1. Save the view and open it in your browser.
  2. Drupal 5 Views Recipes


Recipe notes

  • The Drupal handbook contains a nice view for creating a monthly archive of content. Even nicer, it is something that can be easily imported into your site. Go to, copy the Import this view code block and paste it into admin/build/views/import. Save the view and place the archive block into a region atadmin/build/blocks. The archive view is similar to the one in Step 2 above, but it is based on the node created time rather than a CCK date field. It also sorts in descending order (with the most recent month at the top) and includes all site content, by default.
  • The Views wildcard is commonly set to all. Of course, if Nodes per Page is set to 10, it really only shows 10 nodes at a time and not all of them. It may be preferable to set the wildcard to the word browse, making the URL:<VIEW-NAME>/browse.

Recipe 39: Date Browser

Completed Recipe 34

Views, by default, come with four View types: Full Nodes,Teaser List,Table View,and List View. The View Types selection list is populated by Views style plugins coded in modules. These style plugins add options to the View Types drop-down in both the Page and Block fieldsets. Style plugins can provide dramatic alterations in node formats. We'll have a chance to look at a date browser, a timeline, a views popup, and a calendar, in Recipes 39 - 42.

  1. Edit the browse_workshops view created in Recipe 34. As usual, you may visit either browse_workshops/edit (this URL uses the general theme) or admin/build/views/browse_workshops/edit (this URL uses the administrative theme).
  2. Set the Page View Type to Date: Date Browser and Save the view. A message is displayed:
  3. A date argument must be added to a Date Browser view.

  4. Add the Datestamp: field_workshop_date field to the Arguments fieldset and Save the view. Another message is displayed:
  5. Date Browser arguments must be set to 'Display All Values'.

    The Date module is kindly guiding us through the appropriate settings for the Date Browser view type.

  6. Go back to the Arguments fieldset and set the Default to Display All Values. While there, also set the Option to summarize by month.
  7. Drupal 5 Views Recipes

  8. Save the view and browse to the browse_workshops page. The first page displays the current month, with a pager. Additional links are displayed for next month and previous month.
  9. Drupal 5 Views Recipes

  10. Let's go ahead and add a block to this view. Go to admin/build/views/browse_workshops/edit. Check the Provide Block box and set the block View Type to Date: Date Browser.
  11. Add this block to a sidebar in admin/build/block.

    The date browser module creates a small block with a single link for the current period (the period—Year, Month, Day is determined by the view argument option).

  12. Drupal 5 Views Recipes

Recipe notes

The Date module offers a number of theme functions for modifying output. Date 5.x-2.x helpfully has twice as many theme functions as Date 5.x-1.x. Regardless of which Date module version you are using, one good way to determine the function names and their associated filenames, is via the grep command. Log on to your host. Go to sites/all/modules/date, and enter the following command:

grep -r theme_ *


This article focused on just a few modules, with an emphasis on date. We explored date formats noting a broad range of Drupal locations where the special formatting for PHP dates can be applied. We explored the Exposed filters fieldset including some special filters enabled by the Views Date Range Filter Module. We had a chance to create some summary views. In the next part, we will cover the Timeline and Calendar modules.

You've been reading an excerpt of:

Drupal 5 Views Recipes

Explore Title