How am I able to get the value of the previous year using PHP. Are there any predefined functions for it?
wattostudios
8,60613 gold badges42 silver badges57 bronze badges
asked May 5, 2011 at 10:37
2
try
echo date["Y",strtotime["-1 year"]];
answered May 5, 2011 at 10:47
diEchodiEcho
52.6k41 gold badges172 silver badges242 bronze badges
1
There are many ways, you can either take away the amount of seconds in a year from time[]
like so:
$prevYear = date['Y', time[] - 60*60*24*365 ];
Or if you'd prefer, use the PHPs clever strtotime[]
function:
$prevYear = date['Y', strtotime['-1 year']];
Or even like others have said, if it's from todays year just do date['Y'] -1
answered May 5, 2011 at 10:45
DunhamzzzDunhamzzz
14.4k3 gold badges48 silver badges74 bronze badges
1
Shortest approach:
$lastYear = [int]date["Y"] - 1;
answered Sep 25, 2017 at 10:33
michal.jakubeczymichal.jakubeczy
6,8761 gold badge52 silver badges57 bronze badges
1
function adddate[$vardate,$added]
{
$data = explode["-", $vardate];
$date = new DateTime[];
$date->setDate[$data[0], $data[1], $data[2]];
$date->modify["".$added.""];
$day= $date->format["Y-m-d"];
return $day;
}
echo "Example : " . adddate["2010-08-01","-1 year"];
answered May 5, 2011 at 10:44
jimyjimy
4,7903 gold badges34 silver badges51 bronze badges
you can give a value of the input tag as :
answered Jun 27, 2019 at 22:28
0
If you want to display the entire date exactly 1 year ago, including the month and year:
Kirk Beard
9,17612 gold badges43 silver badges46 bronze badges
answered Jan 24, 2013 at 14:56
Midnightly CoderMidnightly Coder
1,0431 gold badge11 silver badges22 bronze badges
answered Mar 31 at 5:26
Try This
date['Y', strtotime['last year']];
answered Nov 7, 2017 at 9:56
This thread needs an update.
Nowadays you would use the PHP date-time object and do something like this:
$lastYear = new DateTime[];
$lastYear->sub[new DateInterval['P1Y']];
echo $lastYear->format['Y'];
answered Apr 25, 2020 at 17:20
HappyCoderHappyCoder
5,7076 gold badges40 silver badges72 bronze badges