Example CodeHow-ToPHP

Auto Update Copyright Year : HTML JavaScript & More

4 Mins read
Auto Update Copyright Year : HTML JavaScript
Automatic update of copyright year would be a real smart thing to do for any website since its such a trivial thing many developer may not remember to update it manually every year. On a web page you can easily do javascript copyright year update.

Recently I observed this with twitter.com when they forgot to update the Copyright year from 2009 to 2010 for few months. automatic copyright year update will avoid any such mistakes on your site. In this tutorial I am trying to capture most common ways where people may need to update copyright year in a website.

  1. Using JSTL (JavaServer Pages Standard Tag Library)

  2. JSTL provides rich tag support, we can use the core and format taglibs to format the year part of copyright message. Include below to taglibs in your JSP page.

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    

    Now add the below code to the footer of your JSP page.

    <c:set var="now" value="<%=new java.util.Date()%>" />
    Copyright &copy; <fmt:formatDate pattern="yyyy" value="${now}" /> My Company Name
    
  3. Ant – Update at build time

  4. This is a small ant target which can be used in your ant build script and can be used to update the application/company copyright year at build time. The below copyright.xml file extracts the (4 digit) year from system time-stamp and updates the property file that is storing copyright information (e.g. application.properties). copyright.xml

    <project name="copyright" default="copyright">
    
    <target name="init">
     <property name="application.properties.file" value="application.properties"/>
    </target>
    
    
    <target name="copyright" depends="init">
      <tstamp>
          <format property="copyright.year" pattern="yyyy"/>
      </tstamp>
      <echo>Copyright is: ${copyright.year}</echo>
      <propertyfile file="${application.properties.file}">
     <entry  key="app.copyright" value="Copyright © ${copyright.year} My Company Name"/>
      </propertyfile>
    </target>
    
    </project>
    
    

    Content of application.properties before build script is run. application.properties

    app.copyright=Copyright © 2009 My Copyright message
    
    

    To see this sample running save the files (copyright.xml & application.properties) in same directory and run “ant -f copyright.xml” on command line in same directory. The copy right year should be updated in application.properties and the content of this file after running ant command should be something like this.

    #Mon Apr 26 11:15:54 PDT 2010
    app.copyright=Copyright © 2010 My Company Name
    

    How to use this script

    In case your application build.xml is too big and you don’t like to add new target in it then you can save the copyright.xml file as it is, and call it in your existing script as shown below

    
        
      
      
    

    Make sure you update the application.properties file with appropriate file name and correct location. Ideally the update of copyright year should be done at run-time, therefore next few ways are for doing it during run-time using different web languages.

  5. PHP Application

  6. Updating Copyright year in a PHP based application can be really simple. Just add below code in your application footer section.

    <p>Copyright &copy; - <?php echo date('Y'); ?> My Company Name. All Rights Reserved.</p>
    
  7. JavaScript Copyright Year Update

  8. Below small JavaScript code snippet can be used to update copyright year in any web application. Just add below code in you footer section. (Thanks to Helen for suggesting this option in the comments.)

    <p>Copyright &copy; <script language="JavaScript" type="text/javascript">document.write((new Date()).getFullYear());</script> My Company Name</p>
    

    In case you don’t want to use client side date object you can always initialize your JavaScript variable from a server side value.

  9. WordPress

  10. There can be two ways to do this in WordPress

    • By PHP
    • – Since WordPress uses PHP the same code can be used by WordPress blog owners to keep their blogs up to date with copyright year. Copyright message are usually added at the footer of the themes (most of times), so to add this code in your WordPress blog simply go to WordPress Dashboard and browse to the footer of the active theme and add this code then click Update. (If you are not a geek I suggest take a backup of your theme before doing any such change.)

      <p>Copyright &copy; - <?php echo date('Y'); ?> My Company Name. All Rights Reserved.</p>
      
    • By JavaScript
    • Same JavaScript code can also be used in a WordPress theme to update the copyright year automatically. Just copy the below code and add in the WordPrese theme footer.

      <p>Copyright &copy; <script language="JavaScript" type="text/javascript">document.write((new Date()).getFullYear());</script> My Company Name</p>
      
  11. Blogger

  12. Same JavaScript code can also be used in a Blogger templates to update the copyright year automatically. Just copy the below code and go to “Design” > “Edit Html”, and add the code below to the blogger template footer section. (If you are using blogger templates then search for [id=’footer-wrapper’])

    <p>Copyright &copy; <script language="JavaScript" type="text/javascript">document.write((new Date()).getFullYear());</script> My Company Name</p>
    

Please share your thoughts and suggestions in case you know a better way of doing the same thing.

Article Updates

  • Updated on May 2019: Minor changes and updates to the introduction section. Images are updated to HTTPS.

10 Comments

Leave a Reply

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