I'm using a template that has this on the portfolio.tpl:

enter image description here

But I don't want to display the date of node created. So, I added in the content type portfolio a field "date_field" (please see picture)enter image description here

What I need now is to display this field on the tpl, replacing the "$node->created".

This is what I tried:

enter image description here

But it doesn't work.

How can I accomplish this?



<?php print format_date($node->field_date["und"][0]["value"], 'custom','d M Y'); ?>
  • It returns this: Warning: date_timezone_set() expects parameter 1 to be DateTime, boolean given in format_date() – Drii7 Feb 26 '16 at 11:07
  • Just do a print of the date to see what value you have: print render ( $node->field_date["und"][0]["value"] ) ; It seems to me that it is a matter of the format. I have tested the code above and works perfectly. But the value was in seconds, if you already have in date format, you are done with the line I copied before. As for print render $content["field_date"], it won't work. – Cesar Feb 26 '16 at 11:11
  • You don't need to wrap format_date() in a render(). And it is preferable to use [LANGUAGE_NONE] instead of ["und"]. – dmsmidt Feb 26 '16 at 11:13
  • You can use dpm, Devel module or just do echo "<pre>";print_r($node);echo "</pre>" to see all variables available to .tpl.php. Don't be afraid if you see thousands and thousands of lines, for a single page Drupal loads tonns of data and metadata and loads memory as if it were a WBH ( Web-Black-Hole ). – Cesar Feb 26 '16 at 11:13
  • dmsmidt, it depends on the source of your data. If the value is already a date in legible (human readable format), you are right, otherwise you will use format_date. Here the problem is that $content['field_date'] is not available to *.tpl.php, but node->field_date[][][] is. – Cesar Feb 26 '16 at 11:16

Make sure that the field is visible under 'Manage Display' for the view mode you are working with.

If you setup the field to display in a format you like you can also just do this in your template file (instead of the format_date() part):

<?php print render($content['field_date'] ?>

In any case make sure that you also do the following before $content is rendered:

<?php print hide($content['field_date']) ?>
  • Thank you, but what I really want is to display in that specific format date. – Drii7 Feb 26 '16 at 11:09
  • Sidenote: You can add a custom date format in Drupal, which you then can reuse across your site. – dmsmidt Feb 26 '16 at 11:14

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.