The copyright symbol in white

Add Copyright Year Automatically to any Website

  • Adam Douglas

The copyright year on a website is pretty much a given no matter what license one ends up choosing. As the years go by, the copyright should be updated, yet more often then not it ends up getting forgotten. The best way to handle it is, to write a little bit of code to have the year automatically be updated for you. No more having to remember to do it, it’s done from the beginning. Here are some simple code examples to accomplish having the year updated for you automatically in various common web languages.

All the solutions provided below will display the copyright year in two different ways. If the “currentYear” is not equal to the “startYear”, then the “startYear” and “currentYear” will be displayed (e.g. 2004 - 2022). Each example requires the “startYear” variable to be set to the first year of copyright. Else if the current year is the “startYear” then only one year will be displayed (e.g. 2004).

Liquid

Copy the Liquid template language code and paste into the desired file.

1
2
3
4
5
6
7
{% assign startYear = "2004" %}
{% assign currentYear = "now" | date: "%Y" %}
{% if currentYear != startYear %}
    {% capture copyrightYear %}{{ startYear }} - {{ currentYear }}{% endcapture %}
{% else %}
    {% capture copyrightYear %}{{ currentYear }}{% endcapture %}
{% endif %}

Reference the Liquid variable “copyrightYear” where the copyright year is to be displayed.

1
2
3
4
5
<footer>
    <p>
        Copyright &copy; {{ copyrightYear }}
    </p>
</footer>

PHP

Add the following code to the desired PHP file.

1
2
3
4
5
6
7
<?php
function getCopyrightYear(){
    $startYear = 2004;
    $currentYear = date('Y');
    return ($startYear != $currentYear ? $startYear . ' - ' : '') . $currentYear;
}
?>

To display the copyright year call the function getCopyrightYear().

1
2
3
4
5
<footer>
    <p>
        Copyright &copy; <?php echo getCopyrightYear(); ?>
    </p>
</footer>

JavaScript

Add the simple JavaScript code to the footer on a website.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<footer>
    <p>
        Copyright &copy; <script>
            startYear = "2004";
            currentYear = new Date().getFullYear();
            if(currentYear != startYear) {
                copyrightYear = startYear + " - " + currentYear
            }
            else {
                copyrightYear = startYear
            }
            document.write(copyrightYear);
        </script>
    </p>
</footer>

This is post 63 of 100, and is round 2 of the 100 Days To Offload challenge.

    • Simplify PHP code
    • Correct spelling
    • change topic
    • change 100DaysToOffload message
    • fix php code