Always Have Correct Footer Dates

One thing that really bothers me is when the year changes (2008 to 2009) and I see a ton of websites that sport “© 2008” in their footer. So, I wanted to share my php code to handle this case so that my sites are always “up to date.”

<?php

/*
  Given Start Year is 2007
  Pretend Current Year is 2007
    => Output is: "2007"
  Pretend Current Year is 2009
    => Output is: "2007-2009"
*/
function footerDate($startYear, $delim='-') {
  $currYear = date('Y');
  if ( intval($currYear) > intval($startYear) ) {
    return $startYear . $delim . $currYear;
  } else {
    return $startYear;
  }
}

?>

<!-- Examples Usage -->
<p>&copy; <?php echo footerDate('2009'); ?> Joseph Pecoraro</p>
<p>&copy; <?php echo footerDate('2007'); ?> Joseph Pecoraro</p>
<p>&copy; <?php echo footerDate('2007', ' to '); ?> Joseph Pecoraro</p>

And the ugly 1 liner if you only need a little clip of php:

<?php $c=date('Y');echo '2009'.((intval($c)>2009)?' - '.$c:''); ?>

So please, update your footers now so that you never have to update them again! Thanks.

4 Responses

1

Jason Au on February 5, 2009 at 2:46 pm  #

Simple and nice code. Fixed my outdated non-dynamic footer date.

2

T on February 10, 2009 at 1:20 pm  #

Nice… though you forgot your own website!

3

Joseph Pecoraro on February 10, 2009 at 1:22 pm  #

@T: I fixed some websites this morning and I’m fixing the rest when I get to them =).

4

coderjoe on February 12, 2009 at 9:52 am  #

I love that your solution does copyright date ranges. So many people just update the date which isn’t necessarily correct since a copyright notes the year of publication.

Well done.

Add a Comment

search