How do i get this year in php?

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?

How do i get this year in php?

allenski

1,4254 gold badges20 silver badges35 bronze badges

asked Sep 15, 2008 at 15:34

11

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.

Jason

5325 silver badges24 bronze badges

answered Sep 15, 2008 at 15:45

How do i get this year in php?

Erik van BrakelErik van Brakel

22.8k2 gold badges51 silver badges66 bronze badges

6

Larsenal

48.2k42 gold badges146 silver badges216 bronze badges

answered Sep 15, 2008 at 15:35

Daniel PapasianDaniel Papasian

16k6 gold badges29 silver badges32 bronze badges

3

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.

answered Sep 15, 2008 at 22:51

gregmacgregmac

23.7k10 gold badges87 silver badges115 bronze badges

2

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

answered Jan 2, 2013 at 18:03

Thomas KelleyThomas Kelley

10k1 gold badge35 silver badges41 bronze badges

0

How do i get this year in php?

Naveed

40.6k32 gold badges95 silver badges130 bronze badges

answered Sep 15, 2008 at 15:36

chrisbchrisb

2,1901 gold badge20 silver badges23 bronze badges

0

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.

answered Sep 15, 2008 at 15:35

Mark BiekMark Biek

143k54 gold badges155 silver badges200 bronze badges

This one gives you the local time:

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

And this one UTC:

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

How do i get this year in php?

Alfred Huang

16.9k32 gold badges114 silver badges185 bronze badges

answered Sep 15, 2008 at 15:44

How do i get this year in php?

Alexey LebedevAlexey Lebedev

11.8k4 gold badges39 silver badges47 bronze badges

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)

answered Jan 17, 2014 at 15:52

How do i get this year in php?

0

For 4 digit representation:


2 digit representation:


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

How do i get this year in php?

Janyk

5713 silver badges18 bronze badges

answered Oct 16, 2014 at 8:02

How do i get this year in php?

joan16vjoan16v

4,9393 gold badges47 silver badges48 bronze badges

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

How do i get this year in php?

answered Apr 11, 2014 at 7:08

kkarayatkkarayat

3845 silver badges14 bronze badges

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.


answered Feb 9, 2017 at 20:19

How do i get this year in php?

Wael AssafWael Assaf

1,15712 silver badges23 bronze badges


This code should do

answered Feb 15, 2017 at 23:22

How do i get this year in php?

3

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

How do i get this year in php?

answered Oct 26, 2016 at 7:02

saadksaadk

1,17512 silver badges17 bronze badges

use a PHP date() function.

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


answered Dec 28, 2018 at 9:45

Sanu0786Sanu0786

56310 silver badges15 bronze badges

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


answered Aug 30, 2012 at 16:32

PanicGripPanicGrip

1421 silver badge2 bronze badges

2

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

answered Nov 28, 2016 at 2:07

How do i get this year in php?

MilanMilan

3,0711 gold badge32 silver badges44 bronze badges

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

answered Dec 25, 2016 at 0:57

How do i get this year in php?

Ivan BarayevIvan Barayev

2,0075 gold badges25 silver badges29 bronze badges

$dateYear = date('Y');
echo "Current Year: $dateYear";

Current Year: 2022

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

22

answered Jan 3 at 16:50

Mr. CoderxMr. Coderx

4416 silver badges4 bronze badges

Get full Year used:

 

Or get only two digit of year used like this:

 

How do i get this year in php?

Suraj Rao

29.1k11 gold badges95 silver badges100 bronze badges

answered Sep 15, 2017 at 5:07

How do i get this year in php?

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.

How do i get this year in php?

Musa Haidari

2,0415 gold badges29 silver badges52 bronze badges

answered Feb 1, 2021 at 8:31

How do i get this year in php?

1

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.

answered Nov 10, 2017 at 5:55

best shortcode for this section:


answered Mar 15, 2019 at 16:48

How do i get this year in php?

1


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

answered Sep 9, 2017 at 5:51

How do i get this year in php?

imtaherimtaher

4144 silver badges9 bronze badges

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

How do i get this year in php?

Pang

9,193146 gold badges85 silver badges118 bronze badges

answered Apr 1, 2020 at 13:39

Hernán EcheHernán Eche

6,15711 gold badges51 silver badges73 bronze badges

In Laravel

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

In PHP

echo date("Y");

answered Jun 16, 2021 at 9:50

How do i get this year in php?

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:

    
    
  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:

    How do i get this year in php?
    Then on next page go to the tab Disclaimers:

    How do i get this year in php?
    and near the top you will find Copyright year:

    How do i get this year in php?
    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:

    
    
  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.

How do i get this year in php?

Dharman

27.9k21 gold badges75 silver badges127 bronze badges

answered Nov 20, 2020 at 21:11

How do i get this year in php?

allenskiallenski

1,4254 gold badges20 silver badges35 bronze badges

0

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


answered Jan 28, 2021 at 6:28

BilalBilal

2,29021 silver badges42 bronze badges

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

answered Mar 2, 2021 at 12:31

How do i get this year in php?

2

create a helper function and call it

getCurrentYear();

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

answered Sep 8, 2021 at 13:34

How do i get this year in php?

How can I get current year in PHP?

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.

How can get current year start and end date in PHP?

$year = date('Y') - 1; // Get current year and subtract 1 $start = mktime(0, 0, 0, 1, 1, $year); $end = mktime(0, 0, 0, 12, 31, $year);

What does date () do in PHP?

The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways. Note: These functions depend on the locale settings of your server.

How can I get current month in PHP?

php $transdate = date('m-d-Y', time()); echo $transdate; $month = date('m', strtotime($transdate)); if ($month == "12") { echo "
December is the month :)"; } else { echo "
The month is probably not December"; } ?>