Reply to comment

custom format display of CCK Date

Sometimes it pays to read the user contributed notes that are part of the Drupal documentation.  If I had done so intially, I would have been done long ago.

The task was to customize the node display of an event to include a "time" only field.  Since the date and time were stored as part of the same CCK Date field, I needed to somehow format the date value to use the time only format string such as: "g:i a"

I setup the custom format in my drupal admin.

I added my format first:

 

I then selected my formatted "Time Only" format and gave it the "Time Only" label (time_only) machine name.

I then copied the following function found at the bottom of http://drupal.org/node/295105 into one of my custom modules:

<?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
'';
}

?>

 

 

In my node template, I call my function and successfully output a "time only" value:

<?php  print custom_format_cck_date_value($node,'field_gl_date',0,'time_only')?>

  • custom_format_cck_date_value is my new function from above
  • 'field_gl_date is the name of my cck field
  • 0 is the index of this cck field that I want to use
  • time_only is the name of the custom format that I configured in the drupal admin formats.

 

 

I wasted a lot of time with "format_date" function.  Basically, you can't pass a Drupal node to it, you need to pass a proper date object.  The following worked also:  Once I figured out that "format_date" didn't accept your Drupal custom formats AND that it needed a Date object, things went easy.

<?php
     $the_date = strtotime($node->field_gl_date[0]['value']);
    print format_date($the_date,'custom','g:i a');
?>


Another thing that helped me immensely, and probably what 99% of the folks that may read this already know, couldn't have gone far without the following in my template:

<?php print_r($node)?>

This allowed me to see the entire glyph of my node and figure out what I was passing to the various functions.


 

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options