Friday 26 June 2015

Converting a timestamp to PHP date format for a field in a node template


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.

No comments:

Post a Comment