Code Snippet

Home » Code Snippets » PHP » Automatic Copyright Year

Automatic Copyright Year

Current year only

&copy; <?php echo date("Y") ?>

With start year

&copy; 2008-<?php echo date("Y") ?>

Start date with error protection

<?php function auto_copyright($year = 'auto'){ ?>
   <?php if(intval($year) == 'auto'){ $year = date('Y'); } ?>
   <?php if(intval($year) == date('Y')){ echo intval($year); } ?>
   <?php if(intval($year) < date('Y')){ echo intval($year) . ' - ' . date('Y'); } ?>
   <?php if(intval($year) > date('Y')){ echo date('Y'); } ?>
<?php } ?>

Usage:

<?php auto_copyright(); // 2011?>

<?php auto_copyright('2010');  // 2010 - 2011 ?>

Reference URL

Subscribe to The Thread

  1. Very nice! I’ve always done with this JS, never thought of doing it with PHP. Will definately use this in the future.

  2. Wow! I’ve used the date feature of PHP, but had never thought to use it in that instance.

  3. Nice, am now using the date function to store system date to database.

  4. Very simple… Nice when copyrights include two years. But if only one (for example company was established just at present year) that code is unsuitable. More universal is…

    $start_year = 2007;
    if ($start_year == date("Y")) {
    echo $start_year;
    } else {
    echo $start_year."-".date("Y");
    }

    • That’s perfectly good code, it’ll just take the server that bit longer to process, I know people who won’t use the php date function in the copyright because of the extra load time and have a feeling this would send them into shock.

      Personally for ease of use I’d use the single copy date or even just the same year to same year i.e. 2007-2007 until the year changed.

  5. Sorry, forgot to remove tags from previous post, please delete it.

    <?php
    $starting_year = 1945;
    echo ($starting_year==date('Y')) ? date('Y') : $starting_year." - ".date('y');
    ?>

  6. <?php
    // if both years are the same display only the current year ,
    // if they are different display both with an en dash between them
    $startYear = 2011;
    $thisYear = date(‘Y’);
    if ($startYear == $thisYear) {
    echo $startYear;
    } else {
    echo “{$startYear}–{$thisYear}”;
    }
    ?>

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~