I have an imagefield, 'event_image' and printing all possible variables for the field tells me I have timestamp as a possible variable of the image to render. I use:

print '<pre>';
print '</pre>

// renders:
string(10) "1348629688"

... in node--mycustom.tpl.php to get the variables.

Now I can render the timestamp as such:

<?php print $node->field_event_image['und'][0]['timestamp'] ?>

... and it prints a UNIX formatted timestamp as:


I'd like to convert this to a PHP date format. So I am guessing I would need to convert the UNIX timestamp somehow using the PHP gmdate function. Typically I would use something like this for raw php:

  print gmdate("m-d-Y", $mytimestamp);

Which would take my timestamp and output:


... but I just don't know how to do within the context of my drupal 7 field and the node template.


Base on clive's comment below, it turns out a preprocess function is the way to go: So in my theme's template.php file I now have:

function MYTHEME_preprocess_node(&$vars, $hook) {

$node = $vars['node'];
$field_items = field_get_items('node', $node, 'field_event_image');
$vars['upload_date'] = format_date($field_items[0]['timestamp'], 'mdy');


... and then in my custom node template, I have:

<?php print $upload_date; ?>

Note, 'mdy' is a custom format I set in the Drupal UI. That's better than using gmdate I suppose.

  • drupal is in php so all functions in php is valid here too. – Mohammed Shameem Jan 20 '13 at 16:39

As already pointed out by Mohammed Shameem:

<?php print gmdate("m-d-Y", $node->field_event_image['und'][0]['timestamp']) ?>

However, the Drupal way would be:

<?php print format_date($node->field_event_image['und'][0]['timestamp'], 'short'); ?>

The latter construct will render the date in the standard short date format set up for the site. The former will use the format you pick. The Drupal way ensures that the date format is standardized throughout the site.

  • 3
    Actually the Drupal way would be $field_items = field_get_items('node', $node, 'field_event_image'); $vars['event_date'] = format_date($field_items[0]['value'], 'short');, run in a preprocess function ;) – Clive Jan 20 '13 at 17:04
  • These are both nice, thanks! @Clive Why would you favor running this in a preprocess function? Is it a security risk or some other issue with doing this in a node? – Danny Englander Jan 20 '13 at 17:33
  • 3
    It's just good practice to separate logic from display...templates should only really be used to print variables, preprocess functions should be used to prepare them. Also your code becomes more reusable, as other modules/themes in the system will have a chance to override those variable if/when necessary – Clive Jan 20 '13 at 17:47
  • 2
    Also, using format_date() parts of the date are translated in the language set for the content; for example, Jan would become Gen when Italian is set as language, and Monday would become Giovedì. – apaderno Jan 20 '13 at 20:48
  • See my "update" in the original question above for the full code for a preprocess function based on Clive's suggestion. – Danny Englander Jan 21 '13 at 16:09

Another example based on field in user object:

  global $user; 
  $curr_user = user_load($user->uid);
  $field_items = field_get_items('user', $curr_user, 'field_some_date_end');

  $datetime = $field_items[0]['value']; // Unix time
  $formatted = format_date($datetime, 'short'); // Date in short format

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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