1163

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?

10
  • 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). Commented 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" ). Commented Feb 7, 2014 at 17:44
  • 10
    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. Commented 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'. Commented Jun 17, 2015 at 9:04
  • 1
    Keep in mind that it maybe "tacky" in some situations. But in many cases it is "tacky" when you have a site that's obviously not changed in a couple of years, but the year of copyright somehow changes. Copyright refers to something creative being locked in a tangible medium, simply "running" the site doesn't generate copyright; writing the code for it does, as well as adding new content. Commented Oct 25, 2024 at 10:30

18 Answers 18

1465
Answer recommended by PHP Collective

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@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 :)
You could just simply say: date("Y");
Chinese and Japanese years ftw! 2016年 / 二千十六年
If I could do this on Youtube. "Like if you are watching this in <?php echo date("Y"); ?>"
also you can use now()->year ... it is pretty much the same
|
556
<?php echo date("Y"); ?>

3 Comments

short tags are not supported by all servers and there's also this: programmers.stackexchange.com/questions/151661/…
In PHP 5.4, you can freely use short echo tags like the above. They're much nicer in views imho.
@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 :)
221

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.

4 Comments

Shorter (but less readable) version: &copy; <?php echo 2008 != date('Y') ? '2008 - ' . date('Y') : 2008; ?> Me, Inc.
My one line version: <?php echo '&copy; 2008', ($year = gmdate("Y")) !== '2008'? ' - '.$year : '', ' Me, Inc.'; ?>
&copy; <?php echo (fn($year) => date('Y') > $year ? date("{$year}–Y") : $year)(2008) ?> Me, Inc.
&copy; 2008<?php if (date('Y') > 2008) echo '–' . date('Y') ?> Me, Inc.
87

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

Comments

35

https://www.php.net/date

echo date('Y');

Comments

33
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 Comment

just a note on strftime, deprecated as of 8.1.0
23

This one gives you the local time:

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

And this one UTC:

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

Comments

17

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

Comments

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)

1 Comment

The question does not ask for d or m. The advice to use Y has been provided on this page years earlier. This answer adds no new, relevant value to this page.
11
print date('Y');

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

Comments

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

Comments

7

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.

2 Comments

This late answer does not reveal any new insights about how to access the current year. date('Y') was advised nearly a decade earlier.
that's exact duplicate of another answer
6

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

<?=date("Y")?>

3 Comments

Please, don't ever, ever, ever use short-tags again. stackoverflow.com/questions/200640/…
php v5.4.0 - the tag <?= is always available regardless of the short_open_tag ini setting.So now you can use this syntax.
I think @Jelmer confused the short tag (<?) with the shorthand echo tag (<?=). The last one can be generally used.
6

Using Carbon

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

In PHP

echo date("Y");

Comments

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

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

Comments

2
<?php
$current_year = date('Y');
echo $current_year ;
?>

3 Comments

Is there an advantage to setting this to a variable before printing it?
Did the job for me. Is there anything wrong with it?
It's fine, but echo date('Y'); does the same thing and is just as easy to read, without need for a variable and the extra source line.
0
$year = date("Y", strtotime($yourDateVar));

1 Comment

This question is asking about how to generate the CURRENT date, not a custom date. This answer is incorrect/inappropriate.
-3

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

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

1 Comment

seems like Laravel.... however the php date method is very much sufficient

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.