Last updated May 10, 2010. Created on February 27, 2007.
Edited by glass.dimly, KarenS. Log in to edit this page.

To print multiple date formats per node see this handbook page. Like other CCK fields, the date is formatted with the CCK formatter. The standard node display will use the formatter selected in the 'Display Fields' tab you see when you add or edit a field. In Views, you will see a drop-down list of available formatters you can use to control the way the field is displayed in the view. The formatters available are:

  • default
  • long
  • medium
  • short
  • timestamp
  • iso
  • feed
  • ical
  • format_interval

The output of the 'default' formatter is the one you set up on your field settings. The output of 'long', 'medium', and 'short' is your site default or whatever you set in 'Advanced display settings' in your field setup form.

The other formatters are automatic, to create a timestamp, iso date, feed-formatted date, ical-formatted date and to use format_interval on a date.

Date fields may have both a 'From Date' and a 'To Date', so all formatters are pushed through a theme to combine the dates together for display. That theme is located at the bottom of the date.module and looks like the following code. You can copy it to your theme folder and change the name to 'mytheme_date_display_combination', then make any changes you like to the standard display. The code below shows you what information will be available to the theme.

<?php
/**
 *  Theme from/to date combination in the view.
 *
 *  @param $field = the field settings
 *  @param $node = node information, this is not always available and not
 *     always the full node, it depends on what value was provided to the formatter.
 *     Only the nid is always guaranteed to be available.
 *  @param $dates - an array of date information, see explanation for date_field_object() for details.
 *
 *  Useful values:
 *    $field['type_name'] is the content type
 *    $field['type'] is the field type
 *    $node->nid is the node nid, get other node values using node_load($node->nid)
 *    $dates['value']['object']->local->timestamp - the local timestamp for the From date
 *    $dates['value2']['object']->local->timestamp - the local timestamp for the To date
 *    $dates['value']['object']->db->timestamp - the timestamp of the From date database (GMT) value
 *    $dates['value2']['object']->db->timestamp - the timestamp of the To date database (GMT) alue
 *    $dates['formatter'] = formatter name, i.e. 'default';
 *    $dates['value']['formatted'] = formatted From date, i.e. 'February 15, 2007 2:00 pm';
 *    $dates['value2']['formatted'] = formatted To date, i.e. 'February 15, 2007 6:00 pm';
 *
 */
function theme_date_display_combination($field, $dates, $node = NULL) {
 
$date1 = $dates['value']['formatted'];
 
$date2 = $dates['value2']['formatted'];
  if (
$date1 == $date2 || empty($date2)) {
    return
'<span class="date-display-single">'. $date1 .'</span>';
  }
  else {
    return
'<span class="date-display-start">'. $date1 .'</span><span class="date-display-separator"> - </span><span class="date-display-end">'. $date2 .'</span>';
  }
}
?>

There is also a theme for the format_interval formatter:

<?php
/**
 * Theme a format interval for a date element
 *
 *  @param $field = the field settings
 *  @param $node = node information, this is not always available and not
 *     always the full node, it depends on what value was provided to the formatter.
 *     Only the nid is always guaranteed to be available.
 *  @param $dates - an array of date information, see explanation for date_field_object for details.
 *  @return a formatted display
 *
 *  Useful values:
 *    $field['type_name'] - the content type
 *    $field['type'] - the field type
 *    $node->nid - the node nid, get other node values using node_load($node->nid)
 *    $dates['value']['object']->local->timestamp - the local timestamp for the From date
 *    $dates['value2']['object']->local->timestamp - the local timestamp for the To date
 *    $dates['value']['object']->db->timestamp - the timestamp of the From date database (GMT) value
 *    $dates['value2']['object']->db->timestamp - the timestamp of the To date database (GMT) alue
 */
function theme_date_format_interval($field, $dates, $node = NULL) {
 
// Time to compare dates to
 
global $user;
  if (
variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
   
$now = (time() - date("Z")) + $user->timezone;
  }
  else {
   
$now = (time() - date("Z")) + variable_get('date_default_timezone', 0);
  }

 
// Pull local timestamps out of date objects.
 
$timestamp1 = $dates['value']['object']->local->timestamp;
 
$timestamp2 = $dates['value2']['object']->local->timestamp ? $dates['value2']['object']->local->timestamp : $timestamp1;

 
// 1) The date is entirely in the future
 
if ($now < $timestamp1) {
    return
t('!time', array('!time' => format_interval($timestamp1 - $now)));
  }
 
// 2) Ongoing date
 
elseif ($now > $timestamp1 && $now <= $timestamp2) {
   
//return t('Started !time ago', array('!time' => format_interval($now - $timestamp1)));
   
return t('ongoing');
  }
 
// 3) Date is in the past
 
else {
    return
t('!time ago', array('!time' => format_interval($now - $timestamp2)));
  }
}
?>

Both of these themes have the entire date object available in an array that looks like:

<?php
$dates
['value']['formatted'] - the formatted display for the From date
$dates
['value']['object'] - the date object for the From date
$dates
['value2']['formatted'] - the formatted display for the To date
$dates
['value2']['object'] - the date object for the To date
?>

Each of those date objects looks like:

<?php
 date
[object] => stdClass Object (
   [
db] => stdClass Object // the value stored in the database
    
[timestamp] => 1171569600
    
[iso] => 2007-02-15T20:00:00
    
[parts] => Array (
       [
seconds] => 0
      
[minutes] => 0
      
[hours] => 20
      
[mday] => 15
      
[wday] => 4
      
[mon] => 2
      
[year] => 2007
      
[yday] => 45
      
[weekday] => Thursday
      
[month] => February
       
[0] => 1171569600
    
)
   )
   [
local] => stdClass Object // the local representation of that value
    
[timestamp] => 1171548000
    
[iso] => 2007-02-15T14:00:00
    
[parts] => Array (
       [
seconds] => 0
      
[minutes] => 0
      
[hours] => 14
      
[mday] => 15
      
[wday] => 4
      
[mon] => 2
      
[year] => 2007
      
[yday] => 45
      
[weekday] => Thursday
      
[month] => February
      
[0] => 1171548000
    
)
     [
timezone] => US/Central
    
[offset] => -21600
  
)
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.