在使用PHP函數計算上一個月的日期時,發現當前日期如果是31號,例如:2019-12-31時,取上一個月的日期返的是219-12-01。
echo date('Y-m-d', strtotime('last month', strtotime('2019-10-31')))
// output 2019-10-01
查PHP官方文檔發現有很多人反饋這個問題,其中一種解決方法:
echo date('Y-m-d', strtotime('first day of -1 month', strtotime('2019-10-31')))