Date minus 1 year?

前端 未结 8 1958
时光取名叫无心
时光取名叫无心 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 15:08

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

提交回复
热议问题