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())));
相关文章
相关标签/搜索