This code will allow you to bypass the default CCK formatter for each date field. That is, you'll be able to print multiple or different date formats in different places in your node.tpl.php file.

In order to reformat the date, you have to grab it from the node, which gives you the ISO format. Then you must convert this ISO format to a date object, and then send that date over to date_format_date to be formatted in the way that you desire. This snippet can be adapted to print many different forms of a date on a single node page.

  $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;

See theme the date for more information on formatting.

If you want to modify a date based on a custom formatter, you should first retrieve the formatter. This can be done as illustrated beneath, where 'time_only' is the ID of your customer formatter:

<?php
  $date_raw = $field_datetime[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
  $date_formatter = date_formatter_format('time_only', 'field_datetime'); // Retrieves the format
  $formatted_date = date_format_date($date_object,'custom',$date_formatter); 

  print $formatted_date;
?>

Comments

HansBKK’s picture

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.

jooplaan’s picture

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?

momper’s picture

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

momper’s picture

hello

ideas?

thanks momper

916Designs’s picture

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;


?>


Alan D.’s picture

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
zilverdistel’s picture

This actually worked for me ...

eileen’s picture

I'm struggling to make the date field work the way I need because I need two display formats in one node.

Ideally, I want this to display: "Friday, May 13 at 12:30PM ... Two days and twelve hours from now."

Currently, the only way I can achieve it is with two date fields...and absurdly requiring the user to input the same date/time twice. (Which I readily admit is a dumb idea.)

Do you have any helpful advice?

aschiwi’s picture

Use "new DateObject" instead of date_make_date:

<?php
  $date_raw = $node->field_event_datetime['en'][0]['value'];
  $date_object = new DateObject($date_raw);
  $formatted_date = date_format_date($date_object,'short');
  print $formatted_date;
?>

The above code makes use of the "short" date format.

m4olivei’s picture

I was struggling with why this wouldn't work for awhile. The above prints the date in UTC. Usuaully you want a specific timezone, such as the timezone the field was saved. In that case you need to set the DateObject as being UTC, and then change it to the timezone you wish to display. If anyone knows of a better way, love to hear it:

  $date_raw = $node->field_event_datetime['en'][0]['value'];
  $date_object = new DateObject($date_raw, $date_raw['timezone_db']);
  date_timezone_set($date_object, timezone_open($date_raw['timezone']));
  $formatted_date = date_format_date($date_object, 'short');
  print $formatted_date;
jmoughon’s picture

Thanks for the start! The problem with your code is how you are accessing the field array. If you look at the structure of the array $date_raw['timezone'] would actually be field_event_datetime['en'][0]['value']['timezone'] Also, new DateObject did not work for me I used date_make_date
Here is how it should look:

<?php
  $date_raw = $node->field_event_datetime['en'][0]['value'];
  $date_object = date_make_date($date_raw, $node->field_event_datetime['en'][0]['timezone_db']);
  date_timezone_set($date_object, timezone_open($node->field_event_datetime['en'][0]['value']['timezone']));
  $formatted_date = date_format_date($date_object, 'short');
  print $formatted_date;
?>

I did not need the ['en']

selinav’s picture

I created a custom format 'simple' : d/m/Y (without time)
If I do this

$formatted_date = date_format_date($date_object,'simple');

I get something like that : Je, 17/06/2010 - 00:00 instead of 17/06/2010

Are other formats taken in consideration or are 'short', 'medium' and 'long' the only accepted values for the second parameter?

digidev’s picture

I had the same issue. It seems you need to retrieve your custom formatter first. In the example beneath, I had a custom formatter named "time_only", next to the standard long, medium and short.

$date_raw = $field_datetime[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
$date_formatter = date_formatter_format('time_only', 'field_datetime'); // Retrieves the format
$formatted_date = date_format_date($date_object,'custom',$date_formatter); 

If you have no custom formatter defined, but just one to specify in your coding how the format should be, you could use:

date_format_date($date_object,'custom',$date_formatter); 
sdmaxey’s picture

I spent some time having problems with the code immediately above (and now the first example in the main article), and I wanted to help others who might cause themselves similar problems.

In the first line:
$date_raw = $field_datetime[0]['value']; //Get datestamp from cck field, could use $node->whatever

$field_datetime should be changed to the name of your CCK date field. However, in the third line:

$date_formatter = date_formatter_format('time_only', 'field_datetime'); // Retrieves the format

while 'time_only' should be changed to the name of your custom date format, 'field_datetime' should stay as is and should NOT be changed to the name of your CCK date field. Using the name of your CCK date field will return the correct part of the date, using the right presentation formatting (e.g. Month DD, YYYY HH:MM vs YYYY - MM - DD HH:MM:SS according to your preferences); however, it will be only the From date of a from-to pair, and will be in the UTC timezone rather than your site's timezone.

Geijutsuka’s picture

This code works like a charm for days, months and years, but when it comes to times, I can't seem to get it to work.

I have a "from" and "to" datetime field which displays both times in the correct custom format (time_only), but the time is completely wrong. I think it might be defaulting to UTC timezone (it's reading 12:00am as 7:00am, but 5:30pm as 12:30am...??). I've tried sdmaxey's recommendation but all that did was blow out the times.

<?php
//for starting time (time_only format)
$date_raw = $field_event_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
$date_formatter = date_formatter_format('time_only', 'field_event_date'); // Retrieves the format
$formatted_timeone = date_format_date($date_object,'custom',$date_formatter);

//for ending time (time_only format)
$date_raw = $field_event_date[0]['value2']; //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
$date_formatter = date_formatter_format('time_only', 'field_event_date'); // Retrieves the format
$formatted_timetwo = date_format_date($date_object,'custom',$date_formatter); ?>

...and printed where it's needed in the tpl.php file:

<label>Time:&nbsp;</label><?php print $formatted_timeone ?>&ndash;<?php print $formatted_timetwo; ?>

Like I said, works beautifully for the day, month and year, but for some reason not liking the time. I'm trying to figure out the problem and will post a solution for anyone who comes across the same shenanigans.