9

I have used the format_date() in my template files to try and output the date created.

I followed this tutorial http://webwash.net/tutorials/handling-date-and-time-drupal-7

I created a date format 'd, j M y' and date type 'article' and called it through the code:

<?php print format_date($node->changed, 'article') ?> 

however the date that it shows is the date it was updated not the date it was created.

How do I show the creation date of the node ?

18

There is a property on the node object called "created". This is what you are after and can be used in the exact same way you have used "changed".

<?php print format_date($node->created, 'article'); ?>
1
  • YES! it worked. i used this <?php print format_date($node->created, 'article'); ?> much thanks. – Jeremy May 22 '13 at 7:28
2

First Steps

The first step is to enable "Display author and date information" on your content type edit page at:

/admin/structure/types/manage/[your_custom_content_type]

...as shown below:

enter image description here

Get the Data

We can now go retrieve this data in the node.tpl.php file and alter the theming of the post date. Ideally this will be for a custom node template and not the main node.tpl.php file. In my case, I have a content type called blog and I only want the post date to look this way for blog posts. I take node.tpl.php (located in my theme folder) and copy and rename it to node--blog.tpl.php. Note the "double dashes", that's new in Drupal

Find the Code to Replace

Now looking in my new node--blog.tpl.php file, I see the PHP code I am after that I will then alter:

<?php if ($display_submitted): ?>
  <div class="submitted"><?php print $date; ?> -- <?php print $name; ?></div>
  <?php endif; ?>

I want to replace this with some custom PHP code essentially breaking the date up into parts, and getting rid of the author name (though you could easily add this back in and theme it).

Code it

Here is our new code:

<?php if ($submitted) { ?>

   <div class="date-in-parts">
       <span class="day"><?php  echo date("j", $node->created); ?></span>
       <span class="month"><?php echo date("M", $node->created); ?></span>
       <span class="year"><?php echo date("Y", $node->created); ?></span>
    </div><!--//date-in-parts -->

  <?php } ?>

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.