How do I use PHP to get the current year?

0 votes
asked Sep 15, 2008 by jd-graffam

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 out-of-date. How would I make the year update automatically with PHP 4 and PHP 5?

19 Answers

0 votes
answered Sep 15, 2008 by daniel-papasian
<?php echo date("Y"); ?>
0 votes
answered Sep 15, 2008 by mark-biek
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.

0 votes
answered Sep 15, 2008 by chrisb

http://us2.php.net/date

echo date('Y');
0 votes
answered Sep 15, 2008 by alexey-lebedev

This one gives you the local time:

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

And this one UTC:

$year = gmdate('Y'); // 2008
0 votes
answered Sep 15, 2008 by erik-van-brakel

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.

0 votes
answered Sep 15, 2008 by gregmac

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.
0 votes
answered Jan 30, 2012 by panicgrip

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

<?=date("Y")?>
0 votes
answered Sep 2, 2013 by thomas-kelley

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");
0 votes
answered Jan 2, 2014 by gaurav
<?php
$time_now=mktime(date('h')+5,date('i')+30,date('s'));
$dateTime = date('d_m_Y   h:i:s A',$time_now);

echo $dateTime;
?>
0 votes
answered Jan 30, 2014 by isa

You can use the simple PHP date class. It provides many useful methods and functions:

$date = new simpleDate();
echo $date->now()->getYear(); 
echo $date->now()->getMonth();
echo $date->set('2013-01-21')->getDayString();
echo $date->now()->compare('2013-01-21')->isBefore();
...

You can check the library tutorials page for more examples

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...