Current year only
© <?php echo date("Y"); ?>
With start year
© 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 - 2017 ?>
Very nice! I’ve always done with this JS, never thought of doing it with PHP. Will definately use this in the future.
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.
@Dave what kind of abuse would you imagine could happen to the copyright date on a website?
Wow! I’ve used the date feature of PHP, but had never thought to use it in that instance.
Nice, am now using the date function to store system date to database.
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…
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.
“I know people who won’t use the php date function in the copyright because of the extra load time”
For actual real? Do they run their server on a pocket calculator? Asking for a friend..
Sorry, forgot to remove tags from previous post, please delete it.
The following is based on what I use for PHP code to print copyright statements with a start date:
Thanks Chris – works fine for me
<?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}”;
}
?>
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?
It must be a PHP page
Great code. Thanks!
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
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
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.
I’ve used the date feature of PHP, but had never thought to use it in that instance.
Thanks! Works perfectly!
This was great thanks, managed to fix up something that has been bothering me for a long time.
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
$startyear=”2013″;
$copyrightyear=($startyear==date(‘Y’)?$startyear:$startyear.” – “.date(‘Y’));
echo $copyrightyear;
Shorter,
Make the three 2000’s your start year
echo $copyrightyear=(“2000″==date(“Y”)?”2000″:”2000 – “.date(“Y”));
Even Shorter
Complete php Copyright code for starting year 2013
Result:
Copyright © 2013 · YourName · YourWebSite · All rights reserved
Starting Year 2000
Result:
Copyright © 2000 – 2013 · YourName · YourWebSite · All rights reserved
I Used HTML special characters for “©” and “·”(middle dot) did not show here.
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
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:
2.) And use this anywhere you want to display copyright:
And the output of this all is:
Depending on the year, function can look also like this
And that is all.
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.
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
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)
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)
Copyright notices are not necessary to be protected by copyright law. This is universally true across all western nations.
Having an automatically updating copyright year is absolutely stupid. It means that for content you wrote in 2011, you’re telling the world that you wrote it in 2014 (or whatever year it currently is). This can create problems for you if there is ever a dispute and the only proof you have that you are the original copyright owner is the copyright notice on your website. Your copyright notice should reflect the year in which the content was created.
These copyright notices are something that everyone really needs to stop doing, especially when it comes to adding a misleading or downright incorrect year. You should only be putting it in there if there is an actual need to do so. It does absolutely nothing to help you from a legal standpoint and it can only potentially hurt you if the information you provide is incorrect.
Better yet:
Note: this PHP shorthand is not always supported in all configurations.
You will sometimes have to set the short_open_tag PHP directive to “1” in your php.ini file.
http://php.net/manual/en/ini.core.php#ini.short-open-tag
This for lazy ppl xD
hi use this code
Copyright © All rights reserved pbimt.
Thanks…
Thanks, very cool!
Shared from my pages.
copyrights © where i paaste this code in footer.php
All those php starts and stops are cute. It’s good practice to include all your PHP between one set of PHP open and close tags. Even if you just echo all the HTML in php, it would prevent the server from having to open and close php more than once on the page.
date_default_timezone_set(); is required now, example;
i have a php config file with:
then this where i output it in my footer:
Just a note that the snippet titled “Start date with error protection” will not work with it’s default auto value.
intval('auto')
will always evaluate to 0 and so will never be equal to the stringauto
.Aside from that obvious bug, it’s also quite a nasty looking piece of code with a fair bit of fluff. Here’s my functionally equivalent cleaned up version:
Perhaps I spoke too soon about the default auto value bug. However, it only works because it’s doing a loose comparison between the integer 0 and the string ‘auto’, which PHP evaluates to true.
Still, my version removes all the fluff and some unnecessary logic :)
You can use PHP short tags for cleaner code:
And there’s no need to use a character escape for the copyright symbol (unless your site uses a legacy character encoding, other than UTF-8).
This also works when using PHP shorthand:
<?= date("Y"); ?>
Rock on!
My feeling is that a date span should use an n-dash (
–
) not a hyphen (-
). It’s visually and semantically more agreeable (a hyphen being the same glyph as a minus sign). Most style guides would agree, but then it depends on how spans are dealt with across your site. It’s more important to be consistent than to be correct.Hi dude, i have also find out one good example
Dynamic Copyright Date WordPress
I always prefer using WordPress plugin like Automatic Copyrights Shortcode, and let it handle this for years of years after I visit again my site. By the way, nice information though. Thank you.