3 easy ways to print current year in Drupal templates

We've all built them about a hundred times, those copyright lines with the year in the footer.  Here's a few ways to print those with the current year generated, along with any drawbacks involved (looking at you Javascript!).

PHP:

<?php
print date('Y');
?>

 

 

Javascript:

<script type="text/javascript">document.write(new Date().getFullYear());</script>

The issue with Javascript is it uses the local computer's time, not the server's.

 

Twig:

{{ 'now'|date('Y') }}

This twig filter can use the same format options as PHP's date() function! http://twig.sensiolabs.org/doc/2.x/filters/date.html

Add new comment

Joshua Walker