Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 4 or PHP 5?

like image 377
JD Graffam Avatar asked Sep 15 '08 15:09

JD Graffam


People also ask

How get current year from last year in PHP?

php echo date("M d Y", strtotime("-1 year")); ?> Show activity on this post. <? php echo date("Y",strtotime("-1 year")); //last year "2021" echo date("Y"); //current year "2022" echo date("Y-m-d",strtotime("-1 year")); //one year ago "2021-03-31" echo date("Y"); //current date "2022-03-31" ?>

How can I get current date in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.

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

How can I get current date in YYYY MM DD format in PHP?

$date = date("yyyy-mm-dd", strtotime(now));


2 Answers

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.

like image 118
Erik van Brakel Avatar answered Oct 14 '22 00:10

Erik van Brakel


<?php echo date("Y"); ?> 
like image 30
Daniel Papasian Avatar answered Oct 13 '22 23:10

Daniel Papasian