Date minus 1 year?

前端 未结 8 1967
时光取名叫无心
时光取名叫无心 2020-11-27 14:28

I\'ve got a date in this format:

2009-01-01

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

8条回答
  •  感动是毒
    2020-11-27 14:59

    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'));
    

提交回复
热议问题