Php get current year

I want to put a copyright notice in the footer of a website 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?


emang lại date("Y"); – doub1ejaông xã Jul 3 "12 at 19:30
I got a warning using that. Added date_default_timezone_set('UTC'); lớn avoid getting the warning. ('UTC+1' doesn't work... can't tell you much as just starting with PHP). Probably there's some way lớn configure PHPhường 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 mặc định timezone & PHPhường. doesn't lượt thích that. You can either phối the default timezone in the php.ini tệp tin with something lượt thích date.timezone = "America/Los_Angeles" or you can set it at the beginning of your code with something lượt thích 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 lớn aid people who want khổng lồ 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 sầu 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 lớn the site are likely khổng lồ 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 sầu 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 PHPhường. it matters when you want to lớn format your date in a different locale than your mặc định. If so, you have lớn use setlocale & strftime. According to lớn the php manual on date:

To format dates in other languages, you should use the setlocale() và strftime() functions instead of date().

From this point of view, I think it would be best lớn use strftime as much as possible, if you even have sầu a remote possibility of having lớn localize your application. If that"s not an issue, piông xã 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 thail& is 2556. not sure if PHPhường locale takes this into lớn account but in a perfect world it should :) – Dirk Hartzer Waldeông chồng Feb 6 "13 at 12:41
1
You could just simply say: date("Y"); – user352353 Dec 12 "13 at 22:58
Chinese & Japanese years ftw! 2016年 / 二千十六年 – Panzercrisis Aquảng cáo 6 "16 at 14:39
3
If I could bởi 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 và there's also this: programmers.stackexchange.com/questions/151661/… – Dirk Hartzer Waldeck Feb 6 "13 at 12:44
5
In PHPhường 5.4, you can freely use short emang đến tags lượt thích 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 Aquảng cáo 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 (PHPhường 5.3.0+) a compact way to bởi vì it using an anonymous function so you don"t have sầu 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 toàn thân here has referenced the built-in DateTime class:

$now = new DateTime(); $year = $now->format("Y");

or one-liner with class thành viên 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 phản hồi  | 
29

http://us2.php.net/date

emang lại 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 đôi mươi 20 silver badges 23 23 bronze badges
0
Add a comment  | 
28
strftime("%Y");

I love sầu strftime. It"s a great function for grabbing/recombining chunks of dates/times.

Plus it respects locale settings which the date function doesn"t bởi vì.


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 phản hồi  | 
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) và 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:

Chechồng 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 bình luận  | 
10

eđến date("Y") gives you current year, và 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 Aquảng bá 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 phản hồi  | 
8

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

It takes the current date and then you provide a format khổng lồ it

& the format is just going khổng lồ 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 phản hồi  | 
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 khổng lồ avoid short form tags, whether it's This appears khổng lồ be just a repeat of this existing answer. – Pang Aug 12 "trăng tròn at 1:07
Add a comment  | 
7

If your server supports Short Tags, or you use PHPhường 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. darkedeneurope.com/questions/200640/… – Jelmer Dec 25 "12 at 10:25
php v5.4.0 - the tag Add a phản hồi  | 
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 bình luận  | 
6

use a PHP.. date() function.

và the format is just going to lớn be Y. Capital Y is going khổng lồ 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 bình luận  | 
5

BTW... there are a few proper ways how to display site copyright. Some people have sầu tendency lớn make things redundant i.e.: Copyright © have both the same meaning. The important copyright parts are:

**Symbol, Year, Author/Owner & Rights statement.**

Using PHPhường + 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 bình luận  | 
5

For up lớn 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 lượt thích below.

modify("-1 years"); //or +1 or +5 years eđến $now->format("Y"); //& 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 bình luận  | 
4

Get full Year used:

Or get only two digit of year used lượt thích 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 bình luận  | 
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 lớn 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 khổng lồ 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 phản hồi  | 
2

You can use this in footer sections to lớn 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 phản hồi  | 
0
$year = date("Y", strtotime($yourDateVar));
Share
Follow
edited Aug 12 "đôi mươi at 1:07
Pang
8,519 144 144 gold badges 75 75 silver badges 113 113 bronze badges
answered Aquảng bá 1 "đôi mươi at 13:39
Hernán EcheHernán Eđậy
5,417 11 11 gold badges 44 44 silver badges 71 71 bronze badges
Add a bình luận  | 
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 thư mục.

Locate copyright text, expected this khổng lồ be all hard coded but found:

Now we know the year is written somewhere in WordPress admin so locate that to lớn delete the year written text. In WP-Admin, go khổng lồ Options on the left main admin menu:

*
Then on next page go to lớn the tab Disclaimers:

*
và 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 PHPhường. Go back khổng lồ chunk of code in STEPhường 2 found in footer.php and update that lớn this:

Done! Just need to lớn kiểm tra lớn ensure changes have sầu taken effect as expected.

Xem thêm: Kỹ Thuật Trồng Táo Tây Cho Sai Quả, Năng Suất Thu Hoạch Cao, Trồng Táo Tây Từ Hạt

this might not be the same case for many, however we"ve come across this pattern among mỏi quite a number of our client sites and thought it would be best khổng lồ document here.



Chuyên mục: