php 关于使用 strtotime('-1 months')的一个bug

        今天,使用php 日期函数处理数据,发现一个问题,在使用 strtotime("-n months")时不能获取准确数据。

        具体场景是这样的,我一直以为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));


结果输出如下:

冷暖自知一抹茶ck

        细心的小伙伴,可能已经发现了问题,用  2017-08-31  并不能格式化得到  2017-06 的数据;   只能是  2017-08-01  或2017-08才能正确得到数据,必须再转换成这种格式才可以,而不是直接根据当前时间进行格式化;和我认知有出误啊!

        在php的bug反馈中也发现了这样一个文章:https://bugs.php.net/bug.php?id=22486

        大家可以使用上述方法查找,也可以使用如下函数:比如你要查找  2017-08的数据

echo date("Ym", mktime(0,0,0,date('m', time())-3,1,date("Y", time())));

        

后续。。

        http://www.laruence.com/2018/07/31/3207.html 

冷暖自知一抹茶ck
请先登录后发表评论
  • 最新评论
  • 总共0条评论