4

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.

1
  • drupal is in php so all functions in php is valid here too. Commented Jan 20, 2013 at 16:39

2 Answers 2

6

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.

5
  • 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
    Commented Jan 20, 2013 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? Commented Jan 20, 2013 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
    Commented Jan 20, 2013 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ì.
    – avpaderno
    Commented Jan 20, 2013 at 20:48
  • See my "update" in the original question above for the full code for a preprocess function based on Clive's suggestion. Commented Jan 21, 2013 at 16:09
0

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 and acknowledge you have read our privacy policy.

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