JD Graffam
JD Graffam

Reputation: 11655

How do I use PHP to get the 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.

How would I make the year update automatically with PHP

Upvotes: 1155

Views: 1560881

Answers (18)

Humphrey
Humphrey

Reputation: 2807

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

Upvotes: 1

Ayaz Khalid
Ayaz Khalid

Reputation: 135

Using Carbon

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

In PHP

echo date("Y");

Upvotes: 6

Hern&#225;n Eche
Hern&#225;n Eche

Reputation: 6899

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

Upvotes: 0

andcl
andcl

Reputation: 3548

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

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

Upvotes: -3

Sushank Pokharel
Sushank Pokharel

Reputation: 881

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.

Upvotes: 7

imtaher
imtaher

Reputation: 438

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

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

Upvotes: 3

Abdul Rahman A Samad
Abdul Rahman A Samad

Reputation: 1152

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)

Upvotes: 15

Ivan Barayev
Ivan Barayev

Reputation: 2055

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

Upvotes: 7

joan16v
joan16v

Reputation: 5139

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

Upvotes: 17

darwin
darwin

Reputation:

print date('Y');

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

Upvotes: 11

Alexey Lebedev
Alexey Lebedev

Reputation: 12197

This one gives you the local time:

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

And this one UTC:

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

Upvotes: 23

gregmac
gregmac

Reputation: 25251

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.

Upvotes: 220

Erik van Brakel
Erik van Brakel

Reputation: 23800

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.

Upvotes: 1455

Thomas Kelley
Thomas Kelley

Reputation: 10292

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

Upvotes: 85

PanicGrip
PanicGrip

Reputation: 141

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

<?=date("Y")?>

Upvotes: 5

chrisb
chrisb

Reputation: 2210

https://www.php.net/date

echo date('Y');

Upvotes: 35

Daniel Papasian
Daniel Papasian

Reputation: 16423

<?php echo date("Y"); ?>

Upvotes: 554

Mark Biek
Mark Biek

Reputation: 150729

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.

Upvotes: 33

Related Questions