I want to put a copyright notice in the footer of a web site, but I think it's incredibly tacky for the year to be outdated.
How would I make the year update automatically with PHP 4 or PHP 5?
You can use either date or strftime. In this case I'd say it doesn't matter as a year is a year, no matter what (unless there's a locale that formats the year differently?)
For example:
<?php echo date("Y"); ?>
On a side note, when formatting dates in PHP it matters when you want to format your date in a different locale than your default. If so, you have to use setlocale and strftime. According to the php manual on date:
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
From this point of view, I think it would be best to use strftime as much as possible, if you even have a remote possibility of having to localize your application. If that's not an issue, pick the one you like best.
My super lazy version of showing a copyright line, that automatically stays updated:
© <?php
$copyYear = 2008;
$curYear = date('Y');
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?> Me, Inc.
This year (2008), it will say:
© 2008 Me, Inc.
Next year, it will say:
© 2008-2009 Me, Inc.
and forever stay updated with the current year.
Or (PHP 5.3.0+) a compact way to do it using an anonymous function so you don't have variables leaking out and don't repeat code/constants:
©
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?>
Me, Inc.
With PHP heading in a more object-oriented direction, I'm surprised nobody here has referenced the built-in DateTime
class:
$now = new DateTime();
$year = $now->format("Y");
or one-liner with class member access on instantiation (php>=5.4):
$year = (new DateTime)->format("Y");
strftime("%Y");
I love strftime. It's a great function for grabbing/recombining chunks of dates/times.
Plus it respects locale settings which the date function doesn't do.
For 4 digit representation:
<?php echo date('Y'); ?>
2 digit representation:
<?php echo date('y'); ?>
Check the php documentation for more info: https://secure.php.net/manual/en/function.date.php
print date('Y');
For more information, check date() function documentation: https://secure.php.net/manual/en/function.date.php
For up to php 5.4+
<?php
$current= new \DateTime();
$future = new \DateTime('+ 1 years');
echo $current->format('Y');
//For 4 digit ('Y') for 2 digit ('y')
?>
Or you can use it with one line
$year = (new DateTime)->format("Y");
If you wanna increase or decrease the year another method; add modify line like below.
<?PHP
$now = new DateTime;
$now->modify('-1 years'); //or +1 or +5 years
echo $now->format('Y');
//and here again For 4 digit ('Y') for 2 digit ('y')
?>
BTW... there are a few proper ways how to display site copyright. Some people have tendency to make things redundant i.e.: Copyright © have both the same meaning. The important copyright parts are:
**Symbol, Year, Author/Owner and Rights statement.**
Using PHP + HTML:
<p id='copyright'>© <?php echo date("Y"); ?> Company Name All Rights Reserved</p>
or
<p id='copyright'>© <?php echo "2010-".date("Y"); ?> Company Name All Rights Reserved</p
My way to show the copyright, That keeps on updating automatically
<p class="text-muted credit">Copyright ©
<?php
$copyYear = 2017; // Set your website start date
$curYear = date('Y'); // Keeps the second year updated
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?>
</p>
It will output the results as
copyright @ 2017 //if $copyYear is 2017
copyright @ 2017-201x //if $copyYear is not equal to Current Year.
in my case the copyright notice in the footer of a wordpress web site needed updating.
thought simple, but involved a step or more thann anticipated.
Open footer.php
in your theme's folder.
Locate copyright text, expected this to be all hard coded but found:
<div id="copyright">
<?php the_field('copyright_disclaimer', 'options'); ?>
</div>
Now we know the year is written somewhere in WordPress admin so locate that to delete the year written text. In WP-Admin, go to Options
on the left main admin menu:
Then on next page go to the tab Disclaimers
:
and near the top you will find Copyright year:
DELETE the © symbol + year + the empty space following the year, then save your page with Update
button at top-right of page.
With text version of year now delete, we can go and add our year that updates automatically with PHP. Go back to chunk of code in STEP 2 found in footer.php
and update that to this:
<div id="copyright">
©<?php echo date("Y"); ?> <?php the_field('copyright_disclaimer', 'options'); ?>
</div>
Done! Just need to test to ensure changes have taken effect as expected.
this might not be the same case for many, however we've come across this pattern among quite a number of our client sites and thought it would be best to document here.
To get the current year using PHP’s date function, you can pass in the “Y” format character like so:
//Getting the current year using //PHP's date function.
$year = date("Y");
echo $year;
//Getting the current year using //PHP's date function.
$year = date("Y");
echo $year;
The example above will print out the full 4-digit representation of the current year.
If you only want to retrieve the 2-digit format, then you can use the lowercase “y” format character:
$year = date("y"); echo $year; 1 2 $year = date("y"); echo $year; The snippet above will print out 20 instead of 2020, or 19 instead of 2019, etc.
For more pricise in second param in date function strtotime return the timestamp passed by param
// This work when you get time as string
echo date('Y', strtotime("now"));
// Get next years
echo date('Y', strtotime("+1 years"));
//
echo strftime("%Y", strtotime("now"));
With datetime class
echo (new DateTime)->format('Y');
If you are using the Carbon PHP API extension for DateTime, you can achieve it easy:
<?php echo Carbon::now()->year; ?>
echo date("Y");
– doub1ejackphp.ini
file with something likedate.timezone = "America/Los_Angeles"
or you can set it at the beginning of your code with something likedate_default_timezone_set( "America/Los_Angeles" )
. – Joshua Pinter