1117

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

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
JD Graffam
  • 11,275
  • 2
  • 18
  • 6
  • 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 '14 at 00: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" )`. – Joshua Pinter Feb 07 '14 at 17:44
  • cheers Josh. I had taken the second approach because that's the solution I came across first. Good to know what to set in php.ini to have this in effect in all scripts. – justin Feb 08 '14 at 11:01
  • 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. – Stijn de Witt Mar 08 '14 at 17:43
  • @StijndeWitt nailed it. For the record, the copyright year is the date it was published, and really holds no actual value. That said, I accidentally ran into this because I was doing the exact same thing B-) – Cyprus106 Mar 05 '15 at 22:26
  • @Stijn de Witt Although technically true, a copyright notice *full-stop* has no real purpose, copyright is automatic, you don't need to announce it. And given that most websites have a dynamic portion then the 'year it was published' would change on a regular basis (at least if we're including revisions), so having the year in the footer reflect the current year is perfectly sensible, as it's a good indicator that the content is current. Informally that's pretty much all it's used for, to say "This website is still current". – Nathan Hornby Jun 02 '15 at 12:29
  • @NathanHornby I know it's automatic. Therefore the notice has no real legal value and is just informative. Therefore, putting always the current year in there is bad as it is *disinformative* it tells you absolutely nothing. Also, the copyright doesn't automatically apply to all content on the website. Instead it applies to each separate publication from the moment it was published. My point: Put the publication date there, or don't put it there at all. Don't script it to the current date. – Stijn de Witt Jun 02 '15 at 14:44
  • 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'. – Nathan Hornby Jun 17 '15 at 09:04
  • Perhaps they believe changing the date resets the clock, and they are effectively re-publishing the work every day. – mckenzm Apr 27 '21 at 20:39

17 Answers17

1405

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.

Jason
  • 542
  • 5
  • 24
Erik van Brakel
  • 23,220
  • 2
  • 52
  • 66
547
<?php echo date("Y"); ?>
Larsenal
  • 49,878
  • 43
  • 152
  • 220
Daniel Papasian
  • 16,145
  • 6
  • 29
  • 32
  • 35
    short tags are not supported by all servers and there's also this: http://programmers.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php – Dirk Hartzer Waldeck Feb 06 '13 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 '13 at 08: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 '18 at 08:07
216

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.
gregmac
  • 24,276
  • 10
  • 87
  • 118
83

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");
Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
33

http://us2.php.net/date

echo date('Y');
Naveed
  • 41,517
  • 32
  • 98
  • 131
chrisb
  • 2,200
  • 1
  • 20
  • 23
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.

Mark Biek
  • 146,731
  • 54
  • 156
  • 201
18

This one gives you the local time:

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

And this one UTC:

$year = gmdate('Y'); // 2008
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
Alexey Lebedev
  • 11,988
  • 4
  • 39
  • 47
15

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

Janyk
  • 571
  • 3
  • 18
joan16v
  • 5,055
  • 4
  • 49
  • 49
13

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)

Abdul Rahman A Samad
  • 1,062
  • 1
  • 15
  • 21
  • 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. – mickmackusa May 21 '23 at 21:54
10
print date('Y');

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

Janyk
  • 571
  • 3
  • 18
6

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')
?>
Ivan Barayev
  • 2,035
  • 5
  • 24
  • 30
5

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

<?=date("Y")?>
PanicGrip
  • 148
  • 1
  • 2
  • 7
    Please, don't ever, ever, ever use short-tags again. http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Jelmer Dec 25 '12 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 '19 at 08:23
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.
Sagar Devkota
  • 1,295
  • 3
  • 12
  • 25
Sushank Pokharel
  • 869
  • 7
  • 15
3
<?php date_default_timezone_set("Asia/Kolkata");?><?=date("Y");?>

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

imtaher
  • 430
  • 4
  • 9
3

Using Carbon

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

In PHP

echo date("Y");
ashish
  • 3,555
  • 1
  • 20
  • 26
0
$year = date("Y", strtotime($yourDateVar));
Pang
  • 9,564
  • 146
  • 81
  • 122
Hernán Eche
  • 6,529
  • 12
  • 51
  • 76
  • This question is asking about how to generate the CURRENT date, not a custom date. This answer is incorrect/inappropriate. – mickmackusa May 21 '23 at 22:08
-3

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

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

andcl
  • 3,342
  • 7
  • 33
  • 61