• tutorials
  • php
  • make-year-in-copyright-notice-automatically-update-itself

Making year in a copyright notice on a website automatically update itself

The end result should look like this:
Copyright © 2024 mysite.com

1. PHP method

<div> Copyright &copy; <?php echo date('Y'), ' mysite.com'; ?> </div>

Note that I used the echo construct with the few arguments (which are substrings) separated by the comma , in place of concatenating the substrings with the dot operator . and providing it to echo as a single argument. The end result is the same but doing it in this way you can slighty improve performance.


A more evolved one-liner would have the site name in the footer as a constant or variable, set in the configuration file or its value derived from the database instead of literal value:

<div> Copyright &copy; <?php echo date('Y'), ' ', SITE_NAME; ?> </div>

To define this constant you'd need to add the following line to the configuration file of your site:

define('SITE_NAME', 'mysite.com');

2. Javascript method

<div> Copyright &copy; <script>document.write('' + new Date().getFullYear() + '');</script> mysite.com </div>
A note on accessibility
In a scenario when Javascript is turned off in the user's Web browser the copyright line would not contain the year and only be:
Copyright © mysite.com

Also note that Javascript takes the value for Date from the user's machine and if the year is incorrectly set on it, using the Javascript method will display this incorrect date too.

[© 2024] Make year in a copyright notice automatically update itself
This page was last updated on February 26, 2021
Ariano says
A very handy snippet, I've used it so many times already.
Posted on Fri 11th October 2024 2:58 AM
Leave a Comment