Reputation: 650
I've a format date like this
$y = date('Y');
so the output is 2015
but if I try getting previous year using the below code:
$yr = date('Y'($y,'-1 year'));
echo $yr;
I get incorrect output. The output is 1970
.
How to get the previous year?
Upvotes: 0
Views: 1057
Reputation: 4302
Try using strtotime function
<?php
echo date('Y', strtotime('-1 Year'));
?>
Upvotes: 1
Reputation: 2626
You aren't using any library which does natural language processing
. A better option will be to do
$y = date('Y') - 1;
Also, I am worried about the syntax: date('Y'($y...
EDIT
Exactly as I expected, your code doesn't even compile: Parse error: syntax error, unexpected '(' in /scratchpad/index.php on line 3
Try creating a function which does this:
function getYear( $before = 0 ) {
return ( date( 'Y' ) - $before );
}
Even better is to use strtotime
:
function getYear( $context ) {
return date( 'Y', strtotime( $context ) );
}
Then use it something like:
echo ( getYear( '-1 year' ) );
will give: 2014
.
Upvotes: 3