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!
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.
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 ); ?>
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" ?>
Change the upper case 'Y' to lower case 'y'
echo date("y");
PHP documentation
y A two digit representation of a year
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.
http://php.net/manual/en/function.date.php
See this documentation. It's date("y")
.
So a lowercase y instead of Y.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With