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

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

    ehh, its not a bug as one person mentioned. that is the expected behavior as the number of days in a month is often different. The easiest way to get the previous month using strtotime would probably be to use -1 month from the first of this month.

    $date_string = date('Y-m', strtotime('-1 month', strtotime(date('Y-m-01'))));
    

提交回复
热议问题