How to get last year date 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

How to get last year date 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

How to get last year date 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

How to get last year date in php?

Try This

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

answered Nov 7, 2017 at 9:56

How to get last year date 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 get last 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 do I get this year in 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.

How do I get this year start and end in PHP?

$year = date('Y') - 1; // Get current year and subtract 1 $start = mktime(0, 0, 0, 1, 1, $year); $end = mktime(0, 0, 0, 12, 31, $year);

How to get current date in PHP with time?

You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.