php獲取上一個月的開始與結束時間遇到的問題
阿新 • • 發佈:2018-06-06
The php 結束 存在 TE 之前 定義 rto date
改正之前:
$_lastMonthStart = date(‘Y-m-1 00:00:00‘, strtotime("-1 month"));
$_lastMonthEnd = date(‘Y-m-d H:i:s‘, strtotime(‘-1 sec‘, strtotime($_monthStart)));
$preMonthRange = [$_lastMonthStart, $_lastMonthEnd];
想要的結果:上一個月的開始和結束時間這一範圍。
出現的問題描述:5月31號獲取到的上一個時間居然是5月1到5月31!!!正確的應該是4月1到4月30
問題原因分析:
問題出現在5.31這一天減去一個月不存在4.31,所以變成了5.1導致出錯。
解決方法:
在上面定義一個’標準時間‘,即本月的1號。
改正之後:
$standardTime = date(‘Y-m-1‘);
$_lastMonthStart = date(‘Y-m-1 00:00:00‘, strtotime("-1 month", strtotime($standardTime)));
$_lastMonthEnd = date(‘Y-m-d H:i:s‘, strtotime(‘-1 sec‘, strtotime($_monthStart)));
$preMonthRange = [$_lastMonthStart, $_lastMonthEnd];
完美解決問題!
php獲取上一個月的開始與結束時間遇到的問題