How to get previous month and year relative to today, using strtotime and date?

前端 未结 15 986
情书的邮戳
情书的邮戳 2020-11-28 07:48

I need to get previous month and year, relative to current date.

However, see following example.

// Today is 2011-03-30
echo date(\'Y-m-d\', strtotim         


        
15条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 08:29

    If you want the previous year and month relative to a specific date and have DateTime available then you can do this:

    $d = new DateTime('2013-01-01', new DateTimeZone('UTC')); 
    $d->modify('first day of previous month');
    $year = $d->format('Y'); //2012
    $month = $d->format('m'); //12
    

提交回复
热议问题