php 獲取上上個月數據 使用 strtotime('-1 months')的一個bug

      今天,使用php 日期函數處理數據,發現一個問題。php

      具體場景是這樣的,我一直覺得strtotime  格式化當前日期指定日期能夠找到對應的數據,好比我要查找上上個與的數據,由於我要獲取當前時間的上上個月函數

echo $enddate = strtotime("-2 months", strtotime("2017-08-31"));
echo "<br>";
var_dump(date("Y-m-d H:i:s",$enddate));
echo $enddate = strtotime("-2 months", strtotime("2017-09-01"));
echo "<br>";
var_dump(date("Y-m-d H:i:s",$enddate));

echo "<hr>";

echo $enddate = strtotime("-2 months", strtotime("2017-08"));
echo "<br>";
var_dump(date("Y-m-d H:i:s",$enddate));
echo $enddate = strtotime("-2 months", strtotime("2017-09"));
echo "<br>";
var_dump(date("Y-m-d H:i:s",$enddate));

 結果輸出以下:spa

        細心的小夥伴,可能已經發現了問題,用  2017-08-31  並不能格式化獲得  2017-06 的數據;   只能是  2017-08-01  或2017-08才能正確獲得數據,必須再轉換成這種格式才能夠,而不是直接根據當前時間進行格式化;和我認知有出誤啊!blog

       多是php 的一個bug,網上查了一下,也有同窗反饋這樣一問題,,class

       你們能夠使用上述方法查找,也能夠使用以下函數:好比你要查找  2017-08的數據date

echo date("Ym", mktime(0,0,0,date('m', time())-3,1,date("Y", time())));
相關文章
相關標籤/搜索