Making year in a copyright notice on a website automatically update itself
Copyright © 2024 mysite.com
1. PHP method
<div>
Copyright © <?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 © <?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 ©
<script>document.write('' + new Date().getFullYear() + '');</script>
mysite.com
</div>
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.