Get last year in php

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

Get last year in php

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

Get last year in php

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

Get last year in php

Try This

date('Y', strtotime('last year'));

answered Nov 7, 2017 at 9:56

Get last year in php

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

How to display previous year in PHP?

To get the previous year in PHP. first, we need to access the current year using the built-in date("Y") function then we need to subtract it with minus 1. The date("Y") function returns the current year in four-digit(2021) string format according to the user's local time.

How get current year from last year in PHP?

php echo date("M d Y", strtotime("-1 year")); ?> Show activity on this post.

How to get year PHP?

To get the current year using PHP's date function, you can pass in the “Y” format character like so: //Getting the current year using //PHP's date function. $year = date("Y"); echo $year; The example above will print out the full 4-digit representation of the current year.