Display multiple Date formats

Last modified: January 27, 2009 - 19:22

CCK modules allow for many various 'formats' to display your field, and date.module is no exception. Sometimes you may want to use different formats depending on where the field is being displayed (for instance, use 'short' date format on teaser view, 'long' date format on full page view, etc..).

Here is an example node.tpl.php that assumes you have created a CCK Date field entitled 'date', with only one date per node. If you have multiple dates you will want to recurse through $node->content['field_date']['items'] to get each date.

<?php
 
// Copy the date element from the $node Object
 
$element = $node->content['field_date']['items'][0];
 
// Drupal 6:
 
$element = $node->content['field']['field_date']['items'][0];
 
// Create an empty array for this date.
 
$date = array();
 
// Set the default format. You can delete this line if you won't be needing the default format.
 
$date['default'] = $node->field_date[0]['view'];
 
// Create an array of the different formats.  Delete the ones you won't use.
 
$formats = array('short', 'medium', 'long', 'timestamp', 'iso', 'feed', 'ical', 'format_interval');

 
// Create variables for each format, e.g. $short, $medium, $long
 
foreach ($formats as $format) {
   
$element['#formatter'] = $format;
   
$date[$format] = theme('date_formatter_default', $element);
  }
?>


<div class="node">
  <?php if ($teaser): ?>
    <!-- Here's the 'short' date: -->
    <?php print $date['short']; ?>

  <?php else: ?>
    <!-- Here's the 'long' date format: -->
    <?php print $date['long']; ?>

  <?php endif; ?>
</div>

You will obviously want to edit an existing node.tpl.php file and customize the above to suit your purposes. Otherwise you will be sorely disappointed with this snippet.

for D5

HansBKK - October 13, 2008 - 02:50

This section is supposed to be for both D5 and D6?

I couldn't get this syntax to work for a D5 node.tpl.php

Showing alternative syntax would be greatly appreciated. . .

Edit 1:
$node->field_xxxxx_xxx[0]['view']

doesn't work (empty output). substituting 'value' is OK, but outputs ISO format

format_date requires Unix timestamp format, so tried using strtotime()

format_date(strtotime($field_date_event[0]['value']), 'custom', 'l, j F, Y')

had to also add timezone adjustment

Edit 2:

better way:

content_format('field_date_event', $node->field_date_event[0], 'default')

and changed the output format in the field config for the content type

How this could be fit in with the more elegant D6 example above is currently beyond me, but hope someone stuck like me arriving here finds this helpful.

multiple internationalisation formats

jooplaan - December 2, 2008 - 22:14

How about using the i18n module with multiple formats? The format for dates are different languages is different. Can we use the PHP local function in combination somehow? Or how do we get "dezember" (German) or "décembre" for "december" in a multiple language website?

it does not work for me - in

momper - February 4, 2009 - 16:01

it does not work for me - in the creation of a cck input field of the type "date" i can choose between "Date" "Datestamp" "Datetime"? i have a multilingual site with events ...
Is this part of my problem? which one i should choose?

and i saw never something like this in cck before:
$element = $node->content['field']['field_date_event']['items'][0];

i only now something like this:
$element = $node->content['field_date_event']['items'][0];

is there a hint for bug-tracking?

thanks momper

hello ideas? thanks momper

momper - March 28, 2009 - 17:29

hello

ideas?

thanks momper

Simple D6 Example

916Designs - April 5, 2009 - 12:55

In a node template file, for a CCK date field named date:

<?php
  $date_raw
= $field_date[0]['value']; //Get datestamp from cck field, could use $node->whatever
 
$date_object = date_make_date($date_raw); //Format_date wants a date object, so create one
 
$formatted_date = date_format_date($date_object,'medium'); //Medium is set up the way you want at /admin/settings/date-time/formats

 
print $formatted_date;


?>

Drupal CCK 6.2 custom example with timezone support

Alan D. - August 26, 2009 - 00:48

It took me a while to figure this out, so I thought that others may find this useful.

<?php
/**
* Custom formatter for a CCK 6.2 node field.
*
* @usage
*   $event_date = custom_format_cck_date_value($node, 'field_event_date', 0, 'event');
* @param $node
*   The content item object.
* @param $cck_name
*   The CCK field name.
* @param $delta
*   The CCK delta item of interest.
* @param $format
*   The format, either 'short', 'medium', 'long', or the
*   machine name of the custom date format.
*
*   This defaults to 'medium' if the format was not found.
*
* @return
*   The formated string. An empty string is returned if
*   the function could not parse the date.
*/
function custom_format_cck_date_value($node, $cck_name, $delta = 0, $format = 'medium') {
 
$field = $node->$cck_name;
  if (isset(
$field[$delta])) {
   
$value = $field[$delta];
    if (
$date = date_make_date($value['value'], $value['timezone_db'], $value['date_type'])) {
     
$format = date_formatter_format($format, $cck_name);
      @
date_timezone_set($date, timezone_open($value['timezone']));
      return
date_format_date($date, 'custom', $format);
    }
  }
  return
'';
}

?>

Alan Davison
www.caignwebs.com.au

 
 

Drupal is a registered trademark of Dries Buytaert.