Grow your CSS skills. Land your dream job.

Last updated on:

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

Comments

  1. Permalink to comment#

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

    • Dave
      Permalink to comment#

      Here’s a reply on a five-year-old post but hey, I believe I’m adding something useful…

      Just to point it out in case anyone’s thinking of using JS: with a JS method, it’s prone to abuse or great error, as you’d be relying on the end user’s PC to have the right time — unless you were using Ajax to pull the real time from the server.

  2. Permalink to comment#

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

  3. Permalink to comment#

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

  4. Permalink to comment#

    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");
    }

    • Permalink to comment#

      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. jc

    <?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}”;
    }
    ?>

  7. I’ve put the following in my html document:

    Copyright © company name All rights reserved

    but nothing happens!

    explanation appreciated

    why is my php text not displaying to try and explain what is going wrong???

    what charachters do i need to put before my text in order for this web page to display my text and not parse the code?

  8. N
    Permalink to comment#

    Great code. Thanks!

  9. Ryan
    Permalink to comment#

    Peter, your page needs to be saved as a PHP i.e. somepage.php – Also, if your trying to view the php page on your local machine it won’t work, all you get is a blank page. To do this you will need to download & install either WAMP or XAMPP, then copy your php page over to WAMP/XAMPP (make sure they are turned on before trying to view the php page). Once copied, type in to your URL bar in your browser; localhost or http://localhost

  10. Permalink to comment#

    Hey,

    I had to put the following in the htaccess file in the root directory for imbeded php in html files to work.

    RemoveHandler .html .htm
    AddType application/x-httpd-php .php .htm .html

  11. Thanks for the snippet Chris! Had a friend whose WordPress theme used to output the copyright year for him but a core function the theme depended on to output the year changed and created errors for him.

    Thanks to your snippet I was able to fix his flawed theme.

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

  13. Permalink to comment#

    Thanks! Works perfectly!

  14. Permalink to comment#

    This was great thanks, managed to fix up something that has been bothering me for a long time.

  15. tony
    Permalink to comment#

    the scripts posted at the top of the page won’t work unless you place a semi-colon just prior to the closing php tag… like so

  16. Al Bauguess
    Permalink to comment#

    $startyear=”2013″;
    $copyrightyear=($startyear==date(‘Y’)?$startyear:$startyear.” – “.date(‘Y’));
    echo $copyrightyear;

  17. Al Bauguess
    Permalink to comment#

    Shorter,
    Make the three 2000’s your start year

    echo $copyrightyear=(“2000″==date(“Y”)?”2000″:”2000 – “.date(“Y”));

  18. Al Bauguess
    Permalink to comment#

    Even Shorter

    echo (“2000″==date(“Y”)?”2000″:”2000 – “.date(“Y”));

  19. Al Bauguess
    Permalink to comment#

    Complete php Copyright code for starting year 2013

    echo "Copyright © ".("2013"==date("Y")?"2013":"2013 - ".date("Y"))." · YourName · YourWebSite · All rights reserved";

    Result:

    Copyright © 2013 · YourName · YourWebSite · All rights reserved

    Starting Year 2000

    echo "Copyright © ".("2000"==date("Y")?"2000":"2000 - ".date("Y"))." · YourName · YourWebSite · All rights reserved";

    Result:

    Copyright © 2000 – 2013 · YourName · YourWebSite · All rights reserved

    • Al Bauguess
      Permalink to comment#

      I Used HTML special characters for “©” and “·”(middle dot) did not show here.

  20. Permalink to comment#

    Hello,

    Actually this part of the code intval($year) == ‘auto’ is a really bad practice. What you’re doing here is comparing an integer (due to the intval cast) with a string. It works only due to the fact, that you’re using the equal operator, and the string on the right side of equation gets automatically casted to the Integer.

    This might be really confusing to the programming beginners and should rather be avoided.

    Read more at: http://php.net/manual/pl/language.operators.comparison.php

    Last but not least, the code shouldn’t be covered with tags in every line of the function, they should be removed to increase the readability ;)

    Kind regards,

    Rafal

  21. Lukas
    Permalink to comment#

    Hi,
    I have done some modification to use it with WordPress and I enhanced it a little bit.

    1.) Paste this to your theme functions.php file:

    function auto_copyright($year = 'auto', $name){
    $copy = ' &copy; ';
    if(intval($year) == 'auto'){
    $year = date('Y').$copy.$name;
    }
    if(intval($year) == date('Y')){
    echo intval($year).$copy.$name;
    }
    if(intval($year) < date('Y')){
    echo intval($year) . ' - ' . date('Y').$copy.$name;
    }
    if(intval($year) > date('Y')){
    echo date('Y').$copy.$name;
    }
    }

    2.) And use this anywhere you want to display copyright:

    <?php auto_copyright('2014','ADD SOME COPYRIGHT TEXT'); ?>

    And the output of this all is:

    2014 © ADD SOME COPYRIGHT TEXT

    Depending on the year, function can look also like this

    2008 – 2014© ADD SOME COPYRIGHT TEXT

    And that is all.

  22. Hi there!

    Thanks so much for this helpful post. I tried using your code for my headway themed wordpress site that I am starting but I could not get it to work :(
    I am trying to put a Copyright at the footer of my website so that year updates to the current year automatically. I tried using this in the visual editor of headway:

    Copyright @ Premiere Condos, Karen Law

    But it does not work…..do you know what I am doing incorrectly?

    Thank you so much for your time!

    • #Karen Law..its this simple. the foooter.php file. Find the line which references the footer message and within that line create php tags and you are done. Look here:
      Copyright © .

      This will definitely do you a great deal.

  23. Richard

    Hi,

    The function works well but just one question… why does each line contain opening and closing PHP tags? Why not just put the tags around the whole lot? It would be more efficient this way.

    Regards,
    Richard

  24. Chris

    hanks for the info! I know you can’t nest PHP functions within eachother… how would I get this to work for something like this? (I want to next this date php inside the printf php I already have)

    "?php printf( __( '© YEARPHP | NAME')); ?"
    
  25. ExitNode
    Permalink to comment#

    Richard, i feel the author just put the tag in there to let the user know the tags have to be present to allow the function kick in (for .php)

    Also there’s an easier way or what i consider easy, i have actually applied this in many instances whilst parsing output via php.

    //Assign
    $copydate = date(“Y”);

    //Everything else can go in here :)

    //Declare or call it up anywhere you need it
    $message .= “Written by ExitNode © “.$copydate.”\n”;

    Result/Output:
    Written by ExitNode © 2014 ($copydate gives the current year)

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
    ``` 
    <div>
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
    </div>
    ```
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".