PHP code to updating the year automatically

If you forget to update the year in your site’s copyright line, this code will help you update the current year. This is a set-and-forget line of PHP code to updating the year automatically.

<!-- Let's assume this year is 2020, then the code will be as below: -->
Copyright &amp;copy; 2020 <?php if(($y=date("Y")) != 2020) echo "- $y"; ?>

The code date("Y") reads the server’s current date and store in the memory with a variable $y and != 2020 checks if the current date is NOT equal to 2020. If it’s not equal, then echo "- $y" print the current year.

If this year is 2020, it displays:

Copyright © 2020

If this year is 2021, it displays:

Copyright © 2020 - 2021
Share this:

Leave a comment

Your email address will not be published. Non-standard characters in the comment form are not permitted and will be removed. Required fields are marked *