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

Bạn đang xem: Php get current year

How would I make the year update automatically with PHP 4 or PHP 5?


echo date("Y"); – doub1ejack Jul 3 "12 at 19:30
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 0:39
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 7 "14 at 17:44
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 8 "14 at 17:43
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 9:04
 |  Show 5 more comments

29 Answers 29


Active Oldest Votes
1246

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:

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.


Share
Follow
edited Feb 20 "14 at 19:02
Jason
512 5 5 silver badges 24 24 bronze badges
answered Sep 15 "08 at 15:45
Erik van BrakelErik van Brakel
21.4k 2 2 gold badges 48 48 silver badges 66 66 bronze badges
6
4
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 :) – Dirk Hartzer Waldeck Feb 6 "13 at 12:41
1
You could just simply say: date("Y"); – user352353 Dec 12 "13 at 22:58
Chinese and Japanese years ftw! 2016年 / 二千十六年 – Panzercrisis Apr 6 "16 at 14:39
3
If I could do this on Youtube. "Like if you are watching this in " – Luka Jan 2 "17 at 19:42
1
also you can use now()->year ... it is pretty much the same – Ahmed Mansour Jan 6 at 16:22
 |  Show 1 more comments
518

Share
Follow
edited May 15 "10 at 20:03
Larsenal
44.9k 40 40 gold badges 139 139 silver badges 208 208 bronze badges
answered Sep 15 "08 at 15:35
Daniel PapasianDaniel Papasian
15.5k 6 6 gold badges 27 27 silver badges 32 32 bronze badges
3
34
short tags are not supported by all servers and there's also this: programmers.stackexchange.com/questions/151661/… – Dirk Hartzer Waldeck Feb 6 "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 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 "18 at 8:07
Add a comment  | 
206

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

© 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:

© Me, Inc.
Share
Follow
edited Mar 12 "15 at 16:53
answered Sep 15 "08 at 22:51
gregmacgregmac
22.3k 9 9 gold badges 78 78 silver badges 113 113 bronze badges
2
15
Shorter (but less readable) version: © Me, Inc. – hitautodestruct Mar 2 "14 at 14:06
My one line version: – ale5000 Jul 3 "17 at 14:42
Add a comment  | 
72

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");
Share
Follow
edited Aug 20 "13 at 18:58
answered Jan 2 "13 at 18:03
Thomas KelleyThomas Kelley
9,618 1 1 gold badge 34 34 silver badges 41 41 bronze badges
0
Add a comment  | 
29

http://us2.php.net/date

echo date("Y");
Share
Follow
edited May 24 "12 at 12:30
Naveed
38.6k 31 31 gold badges 91 91 silver badges 129 129 bronze badges
answered Sep 15 "08 at 15:36
chrisbchrisb
2,100 1 1 gold badge 20 20 silver badges 23 23 bronze badges
0
Add a comment  | 
28
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.


Share
Follow
answered Sep 15 "08 at 15:35
Mark BiekMark Biek
134k 52 52 gold badges 150 150 silver badges 195 195 bronze badges
Add a comment  | 
14

This one gives you the local time:

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

And this one UTC:

$year = gmdate("Y"); // 2008
Share
Follow
edited Jan 17 "16 at 4:49
Alfred Huang
15k 30 30 gold badges 98 98 silver badges 174 174 bronze badges
answered Sep 15 "08 at 15:44
Alexey LebedevAlexey Lebedev
11.2k 3 3 gold badges 36 36 silver badges 46 46 bronze badges
Add a comment  | 
13

Here"s what I do:

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)


Share
Follow
edited Jul 7 "17 at 7:09
answered Jan 17 "14 at 15:52
Abdul Rahman A SamadAbdul Rahman A Samad
891 13 13 silver badges 18 18 bronze badges
0
Add a comment  | 
12

For 4 digit representation:

2 digit representation:

Check the php documentation for more info: https://secure.php.net/manual/en/function.date.php


Share
Follow
edited May 8 "16 at 13:00
Janyk
560 3 3 silver badges 18 18 bronze badges
answered Oct 16 "14 at 8:02
joan16vjoan16v
4,501 2 2 gold badges 43 43 silver badges 45 45 bronze badges
Add a comment  | 
10

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


Share
Follow
edited Apr 15 "14 at 12:16
Peter Mortensen
27.8k 21 21 gold badges 94 94 silver badges 123 123 bronze badges
answered Apr 11 "14 at 7:08
kkarayatkkarayat
374 5 5 silver badges 14 14 bronze badges
Add a comment  | 
9
print date("Y");

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


Share
Follow
edited May 8 "16 at 12:18
Janyk
560 3 3 silver badges 18 18 bronze badges
answered Sep 15 "08 at 15:37
darwindarwin
Add a comment  | 
8

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.

Xem thêm: " Wf Là Gì ? Các Vị Trí Trong Bóng Đá


Share
Follow
answered Feb 9 "17 at 20:19
Wael AssafWael Assaf
910 11 11 silver badges 19 19 bronze badges
Add a comment  | 
8

This code should do


Share
Follow
edited Oct 21 "17 at 8:18
answered Feb 15 "17 at 23:22
Abdelkader SoudaniAbdelkader Soudani
464 6 6 silver badges 17 17 bronze badges
2
1
Try to avoid short form tags, whether it's This appears to be just a repeat of this existing answer. – Pang Aug 12 "20 at 1:07
Add a comment  | 
7

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


Share
Follow
answered Aug 30 "12 at 16:32
PanicGripPanicGrip
134 1 1 silver badge 2 2 bronze badges
2
6
Please, don't ever, ever, ever use short-tags again. obatambeienwasirherbal.com/questions/200640/… – Jelmer Dec 25 "12 at 10:25
php v5.4.0 - the tag Add a comment  | 
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...


Share
Follow
edited Nov 2 "16 at 17:38
Peter Mortensen
27.8k 21 21 gold badges 94 94 silver badges 123 123 bronze badges
answered Oct 26 "16 at 7:02
saadksaadk
1,077 12 12 silver badges 17 17 bronze badges
Add a comment  | 
6

use a PHP date() function.

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


Share
Follow
answered Dec 28 "18 at 9:45
Sanu0786Sanu0786
564 10 10 silver badges 15 15 bronze badges
Add a comment  | 
5

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:

or

Share
Follow
answered Nov 28 "16 at 2:07
MilanMilan
2,843 1 1 gold badge 29 29 silver badges 43 43 bronze badges
Add a comment  | 
5

For up to php 5.4+

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.

modify("-1 years"); //or +1 or +5 years echo $now->format("Y"); //and here again For 4 digit ("Y") for 2 digit ("y") ?>
Share
Follow
answered Dec 25 "16 at 0:57
Ivan BarayevIvan Barayev
1,825 5 5 gold badges 19 19 silver badges 25 25 bronze badges
Add a comment  | 
4

Get full Year used:

Or get only two digit of year used like this:


Share
Follow
edited Sep 18 "17 at 7:11
Suraj Rao
28k 10 10 gold badges 84 84 silver badges 94 94 bronze badges
answered Sep 15 "17 at 5:07
Ganesh UdmaleGanesh Udmale
133 2 2 silver badges 8 8 bronze badges
Add a comment  | 
4

best shortcode for this section:


Share
Follow
answered Mar 15 "19 at 16:48
Omid AhmadyaniOmid Ahmadyani
1,014 9 9 silver badges 13 13 bronze badges
1
I was gonna write the same :D – Lukas Mar 18 "19 at 13:38
Add a comment  | 
3

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

Copyright ©

It will output the results as

copyright
2017 //if $copyYear is 2017 copyright
2017-201x //if $copyYear is not equal to Current Year.
Share
Follow
edited Mar 29 "18 at 10:39
Sagar Devkota
838 2 2 gold badges 9 9 silver badges 22 22 bronze badges
answered Nov 10 "17 at 5:55
Sushank PokharelSushank Pokharel
743 5 5 silver badges 13 13 bronze badges
Add a comment  | 
2

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


Share
Follow
answered Sep 9 "17 at 5:51
imtaherimtaher
346 1 1 silver badge 8 8 bronze badges
Add a comment  | 
0
$year = date("Y", strtotime($yourDateVar));
Share
Follow
edited Aug 12 "20 at 1:07
Pang
8,519 144 144 gold badges 75 75 silver badges 113 113 bronze badges
answered Apr 1 "20 at 13:39
Hernán EcheHernán Eche
5,417 11 11 gold badges 44 44 silver badges 71 71 bronze badges
Add a comment  | 
0

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:

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:

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.