creative insites header

Customize the date format for the node and view template

October 4th, 2011 Tutorial

This tutorial assumes a knowledge with drupal themes and how to customize them.

In order to customize the date for the node template, a preprocess function must be created in the template.php file.

function yourthemesname_preprocess_node(&$vars) {
$node = $vars['node'];
$vars['date_day'] = format_date($node->created, 'custom', 'j');
$vars['date_month'] = format_date($node->created, 'custom', 'M');
$vars['date_year'] = format_date($node->created, 'custom', 'Y');
}

Then add the variables to your node-xxxx.tpl.php:

<div class="date-block">
    <span class="month"><?php print $date_month; ?></span>
    <span class="day"><?php print $date_day; ?></span>
    <span class="year"><?php print $date_year; ?></span>
</div>

Finally add style it with css.

Customizing the date within a views template
What if you want to do the same thing with a view template? It's pretty much the same thing but there are a few changes.

Create a new template for the "created" field in your view. Add this code:

<?php

    $day

= format_date($row->node_created, 'custom', 'd');
   
$month = format_date($row->node_created, 'custom', 'M');
   
$year = format_date($row->node_created, 'custom', 'Y');
   
?>

<div class="date-block">
<span class="month"><?php print $month; ?></span>
    <span class="day"><?php print $day; ?></span>
    <span class="year"><?php print $year; ?></span>
</div>

Finally style with CSS.