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

前端 未结 15 978
情书的邮戳
情书的邮戳 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:20

    Have a look at the DateTime class. It should do the calculations correctly and the date formats are compatible with strttotime. Something like:

    $datestring='2011-03-30 first day of last month';
    $dt=date_create($datestring);
    echo $dt->format('Y-m'); //2011-02
    

提交回复
热议问题