Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the year in 2 digits in php?

Tags:

php

I want to get the year in 2 digits like 15.

using the php date() function

echo date("Y");

but this returns the full year.

any help is much appriciated. Thanks!

like image 992
patricia perez Avatar asked Feb 18 '15 19:02

patricia perez


People also ask

What is a 2 digit year?

If a data item with a 4-digit year or century is moved to a 2-digit year, the first 2 digits of the year (the century) are truncated.

How can I get the last two digits of a number in php?

You can just address it as string, the function substr will convert it automatically: <? php //$year = '200912'; $year = $flightDates->departureDate->year; echo substr( $year, -2 ); ?>

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" ?>


3 Answers

Change the upper case 'Y' to lower case 'y'

echo date("y");

PHP documentation

y   A two digit representation of a year
like image 64
Rick S Avatar answered Oct 19 '22 07:10

Rick S


You can use substr() method of PHP to get the last two digit of a year as follows.

$year =  2011;
$year = substr( $year, -2);

The above code gives 11 as a output which is the last two digit of 2011.

like image 43
Prakash Bhandari Avatar answered Oct 19 '22 09:10

Prakash Bhandari


http://php.net/manual/en/function.date.php

See this documentation. It's date("y"). So a lowercase y instead of Y.

like image 8
Jeroen Avatar answered Oct 19 '22 07:10

Jeroen