Skip to content
Start » Web development » How to create Auto Year Copyright Date on your blog or website with PHP, WordPress and Laravel

How to create Auto Year Copyright Date on your blog or website with PHP, WordPress and Laravel

Adding the copyright information with the current year in the footer of websites and blogs is important to let your audience know that the content they find is “alive” and updated on this website. 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 for the current year up to date, especially if you take care of many sites, is bad if handled manually, even annually.

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

Follow the ways to implement this, not only using pure PHP, but also in Blade views of sites and systems developed in Laravel or in a function in your WordPress blog theme.

Auto year in PHP

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

Calling gives you and passing the parameter of the format you want, you can specify that only the year, with 4 (four) digits, should be presented as output of the command.

YA full year representation, 4 digitsExamples: 1999 or 2003
Extracted PHP Manual

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

copyright 2010 -

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

Copyright in Blade Laravel

If you are using framework Laravel will use the same function gives you PHP, however, will do this the way the Blade template engine uses to display information and output its user screens.

Just replace the instruction <?php echo .. ?> by the well-known guideline with double keys {{ ... }}

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

Great, now you also keep the copyright year of your Laravel systems up to date.

Automatic copyright footer in WordPress

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

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

For that, in your file functions .php add 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'"); 1TP3 Output = ''; if($dates) { $copyright = "copyright " . $dates[0]->first; if($dates[0]->first != $dates[0]->last) { $copyright .= '-' . $dates[0]->last; } $output = $copyright; } return $output; }

Add to footer.php file

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

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

<?php echo mazerdev_copyright(); ?>

Create a Shortcode

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

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

// note that we changed the name of the function to not collide with the name of the shortcode we want to register function mazerdev_copyright_handle() { // the existing code } // add this line add_shortcode( 'mazerdev_copyright', 'mazerdev_copyright_func' );

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

The default shortcode call is [shortcode_name].

Therefore, to enter automatic copyright you can now use a widget or footer builder that your theme may offer, simply by entering the shortcode [mazerdev_copyright] at the point where you want the year correctly entered.

[mazerdev_copyright]

Result of functions

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

copyright 2010 – 2021

By applying this technique in the environment you are using for your website or blog, you will no longer have your copyright dates out of date.


keep up to date

Don't forget to follow me on twitter and sign the Mazer.dev channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.

Leave a Reply

Your email address will not be published.

en_USEnglish
%d bloggers like this: