PHP Footer Copyright Year

This is a small tutorial / code snippet on how to display the correct copyright year with PHP. In PHP, you can get the current year like so:

<?php

//Will print out the current year.
echo date("Y");

Using PHP’s date function, we can determine what year range we should display in our copyright notice.

Here is the first example:

<?php

//Specify a start year.
$startYear = 2012;
//Get the current year;
$thisYear = date("Y");

//By default, set the date to the start year.
$yearInfo = $startYear;

//If the start year and this year are not the same, add a hyphen.
if($startYear != $thisYear){
    $yearInfo = $startYear . "-" . $thisYear;
}

//Print out the year info
echo $yearInfo;

In the example above, we set 2012 as our “start year”. If you plan on using the code above, you will need to change the $startYear variable to match the year that your website came into existence (or the year that your website started to publish content).

If the $startYear is not the same as the current year, the code will display a date range (Example: 2012-2014). However, if the start year is the same as the current year, then we simply display a single year (Example: 2014).

To make things even simpler, I have created the following copyright function:

<?php

function copyright($name, $startYear = null){
    $thisYear = date("Y");
    if(is_null($startYear) || $thisYear == $startYear){
        return "&copy; ". date("Y") . " $name All Rights Reserved";
    }
    return "&copy; $startYear-" . date("Y") . " $name All Rights Reserved";
}

//Examples
echo copyright('ThisInterestsMe.com', 2011);
echo copyright('ThisInterestsMe.com');
echo copyright('ThisInterestsMe.com', 2014);

As you can see, the first parameter is the name of the website (or your personal name, depending on what you want), whereas the second parameter is the start year. Note that the start year is an optional parameter. If you decide to omit the start year, then you can just provide the name of your website. I’ve also taken the liberty of adding the copyright symbol, as well as the “All Rights Reserved” text.

If you’re not interested in adding functions and you’re wanting to keep it confined to one line of code, you can use the following snippet:

&copy; <? (2012 == date("Y")) ? date("Y") : 2012 . "-" . date("Y"); ?> ThisInterestsMe.com All Rights Reserved

Obviously, you will need to change 2012 to match the start year of your website.

Hopefully, this article helped!