1

How can I call the $created variable in my content type node template? I tried the following but it just produces what seems to be a random string of numbers:

        <?php print render($created); ?>

2 Answers 2

2

The $created variable is just a number, but it's not random it is a unix timestamp of the node created date.

Since it is not a render array you do not print it using the render() function, you would just print it like this:

<?php print $created; ?>

However that is also probably not what you want because you probably want a human readable date/time, not a unix time stamp.

As you can see in the main variable preprocess function for the node template (template_preprocess_node()), there is also a variable $date, which is a formatted date based on the $created variable. That will default to using your website's medium date format.

You can use it like this:

<?php print $date; ?>

If that is not the format you want you can also manually use the format_date() function to format it to your requirements like this:

<?php print format_date($created, 'short'); ?>

or like this for something more custom:

<?php print format_date($created, 'custom', 'd/m/Y'); ?>

See the php date() documentation for more information on custom date format options.

0

This can vary from theme to theme so you can look in your template.php for the hook_preprocess_node(). This is where you can alter and set variables for your node.tpl.php. The $submitted variable could be used or modified in this case.

/**
 * Implements hook_preprocess_node().
 */
function my_theme_preprocess_node(&$variables) {
  // Alter the submitted variable, just on blog type.
  if ($variables['type'] == 'blog') {
    $variables['submitted'] = format_date($variables['created']);
  }
  // Or define a new variable for your node.tpl.php
  $variables['created_date'] = format_date($variables['created']);
}

Print the variable in your node.tpl.php

<?php print $created_date ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.