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 automat
Just write:
date("Y") // A full numeric representation of a year, 4 digits
// Examples: 1999 or 2003
Or:
date("y"); // A two digit representation of a year Examples: 99 or 03
And 'echo' this value...
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')
?>
<?php echo date("Y"); ?>
This code should do
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");
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.
If you are using the Carbon PHP API extension for DateTime, you can achieve it easy:
<?php echo Carbon::now()->year; ?>