Duane Blake

Front end developer

Dynamically generate the current year

It’s coming up to my favorite time of the year where dev go through their site and update the year of the copyright of the site. But this is such a tedious task which you should allow the code to do itself. I’m going to demo a couple ways to update the change the date.

clockkinglarge

I once worked on a project where the previous developer wrote every single copyright date as static HTML. The problem was he did this on over 50 of our sites which we manage it was a very painful exercise to update the years.

PHP

©  Wayne Enterprises <?php echo date(‘Y’); ?>

jQuery


<p class="site-copyright">©  Wayne Enterprises </p>
<script type="text/javascript">
$(document).ready(function(){
  
   var theDate = new Date(),
       theYear = theDate.getFullYear();                         $("<span>").html(theYear).appendTo(".site-copyright");
            });
</script>

Javascript


<p>©  Wayne Enterprises<span id="copyright"></span></p>

<?script type="text/javascript">
var theDate = new Date(),
      theYear = theDate.getFullYear();
            
document.getElementById("copyright").innerHTML = theYear;
</script>

Template Toolkit


[% USE date %]
<p>©  Wayne Enterprises [% date.format(date.now, '%Y') %]</p>

Leave a comment

Your email address will not be published. Required fields are marked *