上次做專案時,發現一個問題,這里記錄一下:
問題:
在使用date函式獲取上一個月最后一天或下個月最后一天時,如果當前日期是31號,獲取的資料有問題,
// 2019-12-01 正確應該是 2019-11-30 date('Y-m-d', strtotime('+1 month', strtotime('2019-10-31'))); // 2019-10-01 正確應該是 2019-09-30 date('Y-m-d', strtotime('-1 month', strtotime('2019-10-31')));
解決辦法:
可以使用“last day of”來獲取最后一天,
// 2019-11-30 date('Y-m-d', strtotime('last day of +1 month', strtotime('2019-10-31'))); // 2019-09-30 date('Y-m-d', strtotime('last day of -1 month', strtotime('2019-10-31')));
測驗后發現獲取第一天資料也有同樣的問題:
相應的可以使用“first day of”來獲取第一天資料,
// 2019-11-01 date('Y-m-d', strtotime('first day of +1 month', strtotime('2019-10-31'))); // 2019-09-01 date('Y-m-d', strtotime('first day of -1 month', strtotime('2019-10-31')));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/107493.html
標籤:PHP
