Adding the copyright information with the current year in the footer of websites and blogs is important to convey to your audience the information that the content they find is “alive” and updated on this site. It is also interesting to add the year of the beginning of the site, the more information about the maturity and evolution of the blog, the more credibility it will pass to the reader.

However, keeping the copyright information in the current year updated, especially if you take care of many sites, is hard if treated manually, even annually.

However, there is a very simple and practical PHP solution that allows you to put on your blog a dynamic and automatic copyright date, so that you no longer have to worry about this situation.

Here are ways to deploy this, not only using pure PHP, but also in Blade views of websites and systems developed in [Laravel][1] or in a function in the theme of your WordPress blog.

Using the PHP date function, which formats the local date and time.

By calling date and passing the parameter of the format you want, you can specify that only the year, with 4 (four) digits should be displayed as output of the command.

  • Parameter Y - full year representation, 4 digits - Examples: 1999 or 2003.

The line of code to be added to your PHP script is as follows:

copyright 2010 - <?php echo date('Y'); ?>

Congratulations your website or blog in pure PHP will present the rights information from the year of beginning to the current date, always updated.

If you are using the Laravel framework you will use the same date function as PHP, but you will do it the way the Blade template engine uses to show information and execute the outputs of your user screens.

Just replace the statement <? php echo .. ?> with the well-known guideline with double curly brackets {{date("Y")}}.

copyright 2010 - {{ date("Y") }}

Great, now you also keep updated the year in the copyright of your systems in Laravel.

Finally, if you’re working with a blog using WordPress, an elegant solution is to create a helper function to be incorporated into your theme.

The purpose of this function is to format the copyright date information in a dynamic and automatic way, however, instead of using a fixed start date in code, inform the date of publication of your first and last articles. If the site is in the first year of life, only the function shows only the current year.

To do this, in your functions.php file add the following code:

function mazerdev_copyright() {
   global $wpdb;
   $dates = $wpdb->get_results("SELECT
                         YEAR(min(post_date_gmt)) AS first,
                         YEAR(max(post_date_gmt)) AS last
                       FROM $wpdb->posts
                       WHERE post_status = 'publish'");
   $output = '';
   
   if($dates) {
      $copyright = "copyright " . $dates[0]->first;

      if($dates[0]->first != $dates[0]->last) {
         $copyright .= '-' . $dates[0]->last;
      }
      
      $output = $copyright;
   }
 
   return $output;
}

The first way to use the copyright function is to insert it directly into the code of your theme, specifically in the file that defines the footer.

To do this, open the footer.php file and enter the following code where you want your date to be shown:

<?php echo mazerdev_copyright(); ?>

Create a Shortcode in Wordpress

The second way, more elegant, better reusable and less intrusive in the code of the theme you are using, is to create a WordPress shortcode.

First you must change the function.php file again by adding the record of your new shortcode:

// see that we changed the name of the function to not clash with the name of the shortcode we want to register
function mazerdev_copyright_handle() {
   // o código já existente
}

// add this line
add_shortcode( 'mazerdev_copyright', 'mazerdev_copyright_func' );

When using the function add_shortcode WordPress will register a shortcode with the name entered in the first parameter, and when it finds the same in the content of your blog, it will execute the function informed in the second parameter.

The default shortcode call is [name_of_shortcode].

Porting, to insert the automatic copyright you can now use a widget or footer builder that your theme can offer, simply by inserting the shortcode [mazerdev_copyright] at the point you want the year correctly inserted.

[mazerdev_copyright]

Result of functions

All functions, demonstrated strategies, will result in the same output, demonstrated below:

<p class="has-background" style="background-color:#e4e7ea">
 copyright 2010 – 2021
</p>

By applying this technique to the environment you are using for your website or blog, you will no longer have your copyright dates outdated.