Display multiple Date formats

Last updated on
30 April 2025

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

Help improve this page

Page status: Not set

You can: