1110

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?

11
  • 716
    echo date("Y");
    – doub1ejack
    Jul 3, 2012 at 19:30
  • 2
    I got a warning using that. Added date_default_timezone_set('UTC'); to avoid getting the warning. ('UTC+1' doesn't work... can't tell you much as just starting with PHP). Probably there's some way to configure PHP to avoid throwing the warnings though (in some config file like php.ini).
    – justin
    Jan 26, 2014 at 0:39
  • 5
    @justin This means you haven't set the default timezone and PHP doesn't like that. You can either set the default timezone in the php.ini file with something like date.timezone = "America/Los_Angeles" or you can set it at the beginning of your code with something like date_default_timezone_set( "America/Los_Angeles" ). Feb 7, 2014 at 17:44
  • 9
    NOTE: The year in a copyright notice does not really have much legal value, but is usually added to aid people who want to know whether the copyright still applies. As such it is supposed to be the year the work was published. Just using the current year really makes no sense whatsoever... However I have seen it done countless times. Mar 8, 2014 at 17:43
  • 8
    I'd personally argue that it has become a web convention, so although you are technically correct, it's not what people expect. The fact remains that although having, i.e. "Copyright 2007, all rights reserved' emblazoned on the footer of a page containing an article written in 2007 is technically correct, visitors to the site are likely to assume that the site has been abandoned. Even large corporations with teams of lawyers still stamp their web pages with the current year, even if it's '2007-2015'. Jun 17, 2015 at 9:04

32 Answers 32

1393

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.

6
  • 5
    @ErikvanBrakel just out of interest the current year in thailand is 2556. not sure if PHP locale takes this into account but in a perfect world it should :) Feb 6, 2013 at 12:41
  • 1
    You could just simply say: date("Y");
    – user352353
    Dec 12, 2013 at 22:58
  • Chinese and Japanese years ftw! 2016年 / 二千十六年 Apr 6, 2016 at 14:39
  • 4
    If I could do this on Youtube. "Like if you are watching this in <?php echo date("Y"); ?>"
    – Luka
    Jan 2, 2017 at 19:42
  • 2
    also you can use now()->year ... it is pretty much the same Jan 6, 2021 at 16:22
546
<?php echo date("Y"); ?>
3
  • 35
    short tags are not supported by all servers and there's also this: programmers.stackexchange.com/questions/151661/… Feb 6, 2013 at 12:44
  • 5
    In PHP 5.4, you can freely use short echo tags like the above. They're much nicer in views imho.
    – Jimbo
    Jul 22, 2013 at 8:50
  • 1
    @ShaneReustle, you missed the semicolons at the end ;) I know they are not important in this case, but it is a good practice for beginners :)
    – Dimitar
    Apr 26, 2018 at 8:07
217

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.
2
  • 18
    Shorter (but less readable) version: &copy; <?php echo 2008 != date('Y') ? '2008 - ' . date('Y') : 2008; ?> Me, Inc. Mar 2, 2014 at 14:06
  • 1
    My one line version: <?php echo '&copy; 2008', ($year = gmdate("Y")) !== '2008'? ' - '.$year : '', ' Me, Inc.'; ?>
    – ale5000
    Jul 3, 2017 at 14:42
81

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

http://us2.php.net/date

echo date('Y');
0
32
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.

1
  • 1
    just a note on strftime, deprecated as of 8.1.0 Oct 12, 2022 at 23:17
17

This one gives you the local time:

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

And this one UTC:

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

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)

0
14

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

11

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

10
print date('Y');

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

10

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"); ?>
9
<?php echo date("Y"); ?>

This code should do

3
  • 2
    Try to avoid short form tags, whether it's <? or <?= because it's not a good practice. It's worth mentioning though that many good frameworks like Zend don't allow shorthand PHP opening tag link So please for my sake perform some keystrokes to save your code readability.
    – Wael Assaf
    Jul 11, 2017 at 19:25
  • 1
    This appears to be just a repeat of this existing answer.
    – Pang
    Aug 12, 2020 at 1:07
  • @Pang or a repeat of the date() function of PHP :) May 8, 2021 at 17:55
9
$dateYear = date('Y');
echo "Current Year: $dateYear";

Current Year: 2022

$dateYear = date('y');
echo $dateYear;

22

8

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

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

<?=date("Y")?>
2
  • 7
    Please, don't ever, ever, ever use short-tags again. stackoverflow.com/questions/200640/…
    – Jelmer
    Dec 25, 2012 at 10:25
  • php v5.4.0 - the tag <?= is always available regardless of the short_open_tag ini setting.So now you can use this syntax.
    – nektobit
    Apr 13, 2019 at 8:23
7

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

7

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
7

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

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;

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.

1
  • 1
    Yes. Thanks. This is Great @Musa Oct 21, 2021 at 7:28
6

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
?>
4

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

best shortcode for this section:

<?= date("Y"); ?>
1
  • I was gonna write the same :D
    – Lukas
    Mar 18, 2019 at 13:38
3
<?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?>

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

3

In Laravel

$date = Carbon::now()->format('Y');
return $date;

In PHP

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

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.

0
1

create a helper function and call it

getCurrentYear();

function getCurrentYear(){
    return now()->year;
}
0

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

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

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');
2
  • 1
    Please share more details. How should that be more precise than date('Y')? Which cases work with your answer, but not with the one that is nearly thirteen years old?
    – Nico Haase
    Apr 20, 2021 at 19:46
  • 1
    first, why strtotime("now") and not time()? second why adding strtotime("now") at all when the default parameter of the date function is the current time? I see a lot of people using this and believing this is more accurate, but in reality, you are adding one more function call, with string comparison, so in short you are making your code slower.
    – Pedja
    Apr 14, 2022 at 10:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.