Date minus 1 year?

I have a date in this format:

2009-01-01 

How to return the same date, but 1 year ago?

+71
date php
Jan 02 '09 at 1:43
source share
7 answers

You can use strtotime :

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

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

 echo date('Ym-d', $date); // echoes '2009-01-01' 
+107
Jan 02 '09 at 1:45
source share

Use the strtotime () function:

  $time = strtotime("-1 year", time()); $date = date("Ymd", $time); 
+78
Jan 02 '09 at 1:45
source share

Using a DateTime Object ...

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

Or use now for today

 $time = new DateTime('now'); $newtime = $time->modify('-1 year')->format('Ym-d'); 
+39
Sep 10 '13 at 8:34
source share

the easiest way I used and worked well

 date('Ym-d', strtotime('-1 year')); 

it worked perfectly .. hope this helps someone too ... :)

+19
Oct 26 '16 at 6:58
source share
 // 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("Ymd", $lastyear); 
+9
Jan 02 '09 at 1:57
source share

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

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

After that, compare only the dates wo.

Hope this can help someone.

0
Dec 05 '18 at 8:09
source share

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

  function yearstodate($years) { $now = date("Ymd"); $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 above you can also use the following

 $user_age_min = "-"."1"; echo date('Ym-d', strtotime($user_age_min.'year')); 
-2
Aug 26 '17 at 11:37 on
source share



All Articles