Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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>';
var_dump(get_defined_vars());
print '</pre>

// renders:
["timestamp"]=>
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:

1348629688

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:

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

Which would take my timestamp and output:

09-26-2012

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

UPDATE:

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.

share|improve this question
    
drupal is in php so all functions in php is valid here too. –  Mohammed Shameem Jan 20 '13 at 16:39

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer
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ì. –  kiamlaluno 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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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