[php] How do I use PHP to get the current year?

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?

This question is related to php date php-5.3 php-5.4 php4

The answer is


<?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?>

You can use this in footer sections to get dynamic copyright year


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.


If you are using the Carbon PHP API extension for DateTime, you can achieve it easy:

<?php echo Carbon::now()->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.

  1. Open footer.php in your theme's folder.

  2. Locate copyright text, expected this to be all hard coded but found:

    <div id="copyright">
        <?php the_field('copyright_disclaimer', 'options'); ?>
    </div>
    
  3. 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:

    enter image description here Then on next page go to the tab Disclaimers:

    enter image description here and near the top you will find Copyright year:

    enter image description here DELETE the © symbol + year + the empty space following the year, then save your page with Update button at top-right of page.

  4. 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">
        &copy;<?php echo date("Y"); ?> <?php the_field('copyright_disclaimer', 'options'); ?>
    </div>
    
  5. 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.


http://us2.php.net/date

echo date('Y');

This one gives you the local time:

$year = date('Y'); // 2008

And this one UTC:

$year = gmdate('Y'); // 2008

My super lazy version of showing a copyright line, that automatically stays updated:

&copy; <?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:

&copy; 
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?> 
Me, Inc.

use a PHP date() function.

and the format is just going to be Y. Capital Y is going to be a four digit year.

<?php echo date("Y"); ?>

My super lazy version of showing a copyright line, that automatically stays updated:

&copy; <?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:

&copy; 
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?> 
Me, Inc.

<?php echo date("Y"); ?>

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


If your server supports Short Tags, or you use PHP 5.4, you can use:

<?=date("Y")?>

use a PHP date() function.

and the format is just going to be Y. Capital Y is going to be a four digit year.

<?php echo date("Y"); ?>

print date('Y');

For more information, check date() function documentation: https://secure.php.net/manual/en/function.date.php


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.


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

<?php
$time_now=mktime(date('h')+5,date('i')+30,date('s'));
$dateTime = date('d_m_Y   h:i:s A',$time_now);

echo $dateTime;
?>

Get full Year used:

 <?php 
    echo $curr_year = date('Y'); // it will display full year ex. 2017
?>

Or get only two digit of year used like this:

 <?php 
    echo $curr_year = date('y'); // it will display short 2 digit year ex. 17
?>

best shortcode for this section:

<?= date("Y"); ?>

echo date('Y') gives you current year, and this will update automatically since date() give us the current date.


<?php echo date("Y"); ?>

http://us2.php.net/date

echo date('Y');

<?php echo date("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.


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'>&copy; <?php echo date("Y"); ?> Company Name All Rights Reserved</p>

or

<p id='copyright'>&copy; <?php echo "2010-".date("Y"); ?> Company Name All Rights Reserved</p

<?php echo date("Y"); ?>

This code should do


If you are using the Carbon PHP API extension for DateTime, you can achieve it easy:

<?php echo Carbon::now()->year; ?>


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')
?>

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


<?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?>

You can use this in footer sections to get dynamic copyright 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.

  1. Open footer.php in your theme's folder.

  2. Locate copyright text, expected this to be all hard coded but found:

    <div id="copyright">
        <?php the_field('copyright_disclaimer', 'options'); ?>
    </div>
    
  3. 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:

    enter image description here Then on next page go to the tab Disclaimers:

    enter image description here and near the top you will find Copyright year:

    enter image description here DELETE the © symbol + year + the empty space following the year, then save your page with Update button at top-right of page.

  4. 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">
        &copy;<?php echo date("Y"); ?> <?php the_field('copyright_disclaimer', 'options'); ?>
    </div>
    
  5. 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.


My way to show the copyright, That keeps on updating automatically

<p class="text-muted credit">Copyright &copy;
    <?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.

$year = date("Y", strtotime($yourDateVar));

This one gives you the local time:

$year = date('Y'); // 2008

And this one UTC:

$year = gmdate('Y'); // 2008

My way to show the copyright, That keeps on updating automatically

<p class="text-muted credit">Copyright &copy;
    <?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.

print date('Y');

For more information, check date() function documentation: https://secure.php.net/manual/en/function.date.php


best shortcode for this section:

<?= date("Y"); ?>

http://us2.php.net/date

echo date('Y');

Here's what I do:

<?php echo date("d-m-Y") ?>

below is a bit of explanation of what it does:

d = day
m = month
Y = year

Y will gives you four digit (e.g. 1990) and y for two digit (e.g. 90)


print date('Y');

For more information, check date() function documentation: https://secure.php.net/manual/en/function.date.php


use a PHP function which is just called date().

It takes the current date and then you provide a format to it

and the format is just going to be Y. Capital Y is going to be a four digit year.

<?php echo date("Y"); ?>

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.


Print current month with M, day with D and year with Y.

<?php echo date("M D 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.


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'>&copy; <?php echo date("Y"); ?> Company Name All Rights Reserved</p>

or

<p id='copyright'>&copy; <?php echo "2010-".date("Y"); ?> Company Name All Rights Reserved</p

<?php
$time_now=mktime(date('h')+5,date('i')+30,date('s'));
$dateTime = date('d_m_Y   h:i:s A',$time_now);

echo $dateTime;
?>

This one gives you the local time:

$year = date('Y'); // 2008

And this one UTC:

$year = gmdate('Y'); // 2008

echo date('Y') gives you current year, and this will update automatically since date() give us the current date.


Get full Year used:

 <?php 
    echo $curr_year = date('Y'); // it will display full year ex. 2017
?>

Or get only two digit of year used like this:

 <?php 
    echo $curr_year = date('y'); // it will display short 2 digit year ex. 17
?>

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')
?>

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.


My super lazy version of showing a copyright line, that automatically stays updated:

&copy; <?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:

&copy; 
<?php call_user_func(function($y){$c=date('Y');echo $y.(($y!=$c)?'-'.$c:'');}, 2008); ?> 
Me, Inc.

<?php echo date("Y"); ?>

This code should do


If your server supports Short Tags, or you use PHP 5.4, you can use:

<?=date("Y")?>

This one gives you the local time:

$year = date('Y'); // 2008

And this one UTC:

$year = gmdate('Y'); // 2008

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

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


use a PHP function which is just called date().

It takes the current date and then you provide a format to it

and the format is just going to be Y. Capital Y is going to be a four digit year.

<?php echo date("Y"); ?>

Print current month with M, day with D and year with Y.

<?php echo date("M D Y"); ?>

Here's what I do:

<?php echo date("d-m-Y") ?>

below is a bit of explanation of what it does:

d = day
m = month
Y = year

Y will gives you four digit (e.g. 1990) and y for two digit (e.g. 90)


$year = date("Y", strtotime($yourDateVar));

<?php echo date("Y"); ?>

print date('Y');

For more information, check date() function documentation: https://secure.php.net/manual/en/function.date.php


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


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to date

How do I format {{$timestamp}} as MM/DD/YYYY in Postman? iOS Swift - Get the Current Local Time and Date Timestamp Typescript Date Type? how to convert current date to YYYY-MM-DD format with angular 2 SQL Server date format yyyymmdd Date to milliseconds and back to date in Swift Check if date is a valid one change the date format in laravel view page Moment js get first and last day of current month How can I convert a date into an integer?

Examples related to php-5.3

How to fix error with xml2-config not found when installing PHP from sources? curl posting with header application/x-www-form-urlencoded PHP Composer behind http proxy How do I use PHP to get the current year?

Examples related to php-5.4

Set port for php artisan.php serve How do I use PHP to get the current year?

Examples related to php4

Generate preview image from Video file? How do I use PHP to get the current year?