At the bottom of most blogs you will find the commonplace copyright notice that may look something like this:

Copyright © 2010 WP First Aid All rights reserved.

Which begs the question, what about those posts made before the year 2010? This is where a dynamic copyright function would come in very handy. It would be more correct to have this display in the footer:

Copyright © 2009-2010 WP First Aid All rights reserved.

Most WordPress themes will include, at a minimum, code that will generate a current year copyright statement, perhaps something along these lines:

<?php _e( 'Copyright &copy; ' ); ?>
<?php echo date( 'Y' ); ?>
<strong> <?php bloginfo( 'name' ); ?> </strong>
<?php _e( 'All rights reserved.' ); ?>

To make this code into a function and add some dynamics lets start with a simple PHP construct:

function dynamic_copyright() {
  // code goes here
}

To get the first year a post was written on the blog we need to find the first post. This can be done with this code snippet:

  $all_posts = get_posts( 'order=ASC' );
  $first_post = $all_posts[0];
  $first_date = $first_post->post_date_gmt;

We started by getting all of the posts with the get_posts() function. We assigned the first post, which is found at index 0 (zero) to its own variable. Then we assign that post’s date (in GMT) to its own variable.

We now have isolated the date of the first post on the blog and can perform further operations on it. The basic dynamic part of the function is complete. If the first post is removed and the “new” first post has a different date, then the above snippet will simply use the new post’s date.

The balance of the function is only a slight modification of the original “minimum” sample code from above:

  _e( 'Copyright &copy; ' );
  echo substr( $first_date, 0 ,4 ) . "-" . date( 'Y' );
  echo ' <strong>' . get_bloginfo( 'name' ) . '</strong> ';
  _e( 'All rights reserved.' );

The key is highlighted at line 6. We echo, or display, the first four characters of the $first_date string which is the year; add a hyphen; and, then call a standard PHP function to add the current year as a four digit representation.

Here is the entire dynamic_copyright() function, including comments:

function bns_dynamic_copyright() {
  /* Get all posts */
  $all_posts = get_posts( 'post_status=publish&order=ASC' );
  /* Get first post */
  $first_post = $all_posts[0];
  /* Get date of first post */
  $first_date = $first_post->post_date_gmt;

  /* Display common footer copyright notice */
  _e( 'Copyright &copy; ' );
  /* Display first post year and current year */
  if ( substr( $first_date, 0, 4 ) == date( 'Y' ) ) {
  /* Only display current year if no posts in previous years */
    echo date( 'Y' );
  } else {
    echo substr( $first_date, 0, 4 ) . "-" . date( 'Y' );
  }
  /* Display blog name from 'General Settings' page */
  echo ' <strong>' . get_bloginfo( 'name' ) . '</strong> ';
  _e( 'All rights reserved.' );
}

Of course, this function is compliments of BuyNowShop.com and has their customary bns prefix as it will be appearing in all of their themes.

Just like I did, you can copy and paste the entire sample bns_dynamic_copyright() code above into your theme’s functions.php file; then, replace the code in your theme’s footer.php file that generates your old static copyright notice with this line of code:

<?php bns_dynamic_copyright(); ?>

Your theme will now display a dynamic copyright notice as you continue your writings for many years to come. Congratulations!