Code Snippet
Automatic Copyright Year
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 - 2011 ?>
Very nice! I’ve always done with this JS, never thought of doing it with PHP. Will definately use this in the future.
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…
$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.
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');
?>
The following is based on what I use for PHP code to print copyright statements with a start date:
<p>Copyright © <?php
$current_year=date("o");
if ($current_year=="2010")
echo "2010";
else
echo "2010 - $current_year"; ?> <acronym title="Cascading Style Sheets">CSS</acronym>-Tricks. All rights reserved.</p>
<?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}”;
}
?>
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.