110

I've got a date in this format:

2009-01-01

How do I return the same date but 1 year earlier?

elitalon
  • 9,191
  • 10
  • 50
  • 86
ajsie
  • 77,632
  • 106
  • 276
  • 381
  • 3
    Don't forget to tend to the semantic issue of what you mean by "one year" w.r.t. leap years. Subtracting 365 days from 2008-02-28 will give you 2007-02-28, while subtracting 365 days from 2008-02-29 will give you 2007-03-31. – HostileFork says dont trust SE Jan 02 '10 at 02:16
  • I guess that it very much depends on what "subtracting a year" means. You could mean the same month and day but one year earlier or the month and day after subtracting 365 days as Hostile points out. – D.Shawley Jan 02 '10 at 02:33

8 Answers8

147

You can use strtotime:

$date = strtotime('2010-01-01 -1 year');

The strtotime function returns a unix timestamp, to get a formatted string you can use date:

echo date('Y-m-d', $date); // echoes '2009-01-01'
Blake Erickson
  • 755
  • 1
  • 9
  • 28
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
121

Use strtotime() function:

$time = strtotime("-1 year", time());
$date = date("Y-m-d", $time);
Dexter
  • 7,911
  • 4
  • 41
  • 40
Alex
  • 3,140
  • 2
  • 22
  • 23
64

Using the DateTime object...

$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');

Or using now for today

$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');
darrenwh
  • 640
  • 5
  • 4
40

an easiest way which i used and worked well

date('Y-m-d', strtotime('-1 year'));

this worked perfect.. hope this will help someone else too.. :)

saadk
  • 1,243
  • 14
  • 18
10

On my website, to check if registering people is 18 years old, I simply used the following :

$legalAge = date('Y-m-d', strtotime('-18 year'));

After, only compare the the two dates.

Hope it could help someone.

Meloman
  • 3,558
  • 3
  • 41
  • 51
10

Although there are many acceptable answers in response to this question, I don't see any examples of the sub method using the \Datetime object: https://www.php.net/manual/en/datetime.sub.php

So, for reference, you can also use a \DateInterval to modify a \Datetime object:

$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));

echo $date->format('Y-m-d');

Which returns:

2008-01-01

For more information about \DateInterval, refer to the documentation: https://www.php.net/manual/en/class.dateinterval.php

Oliver Tappin
  • 2,511
  • 1
  • 24
  • 43
9
// set your date here
$mydate = "2009-01-01";

/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));

// format and display the computed date
echo date("Y-m-d", $lastyear);
Nirmal
  • 9,391
  • 11
  • 57
  • 81
-4

You can use the following function to subtract 1 or any years from a date.

 function yearstodate($years) {

        $now = date("Y-m-d");
        $now = explode('-', $now);
        $year = $now[0];
        $month   = $now[1];
        $day  = $now[2];
        $converted_year = $year - $years;
        echo $now = $converted_year."-".$month."-".$day;

    }

$number_to_subtract = "1";
echo yearstodate($number_to_subtract);

And looking at above examples you can also use the following

$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));