这都可以(php获取当前时间)php获取月份,php获取上个月,上上个月的开始时间与结束时间的方法,php获取上个月,上上个月的开始时间与结束时间的方法,
记录一下利用php脚本获取上个月,上上个月的开始时间与结束时间的方法,下面的示例代码,有需要的可以参考一下。
php 获取上个月,上上个月的开始时间与结束时间
php获取上个月以及上上个月的时间,就需要从当前的时间减去一个月时间段就可以。我们可以使用 php 中的 strtotime 函数,strtotime 函数可以很方便的在一个时间的基码上进行时间的减去与增加,比如减去一个月,减去一天等等。
示例代码:
//上个月
echo 上个月:.date(Y-m-01, strtotime(-1 month));
echo <br/>;
$time = strtotime(-1 month);
echo date(Y-m-d H:i:s, mktime(0,0,0,date(m,$time),01,date(Y,$time)) ); //php上个月的开始时间
echo <br/>;
echo date(Y-m-d H:i:s, mktime(23,59,59,date(m,$time),date(t,$time),date(Y,$time)) ); //上个月的结束时间
echo <hr>;
//上上个月
echo 上上个月:.date(Y-m-01, strtotime(-2 month));
echo <br/>;
echo date(Y-m-01, strtotime(-2 month));
$time2 = strtotime(date(Y-m-01, strtotime(-2 month)));
echo <br/>;
echo date(Y-m-d H:i:s, mktime(0,0,0,date(m,$time2),01,date(Y,$time2)) );
echo <br/>;
echo date(Y-m-d H:i:s, mktime(23,59,59,date(m,$time2),date(t,$time2),date(Y,$time2)) );
echo <hr>;
//上上上个月
echo 上上上个月:. date(Y-m-01, strtotime(-3 month));
$time3 = strtotime(date(Y-m-01, strtotime(-3 month)));
echo <br/>;
echo date(Y-m-d H:i:s, mktime(0,0,0,date(m,$time3),01,date(Y,$time3)) );
echo <br/>;
echo date(Y-m-d H:i:s, mktime(23,59,59,date(m,$time3),date(t,$time3),date(Y,$time3)) );
// 飞鸟慕鱼博客 feiniaomy.com复制
注意:每个月的结束时间,是按的当月最后一天晚上23点59分59秒来算的。
本文链接:http://blog.sauo.top/?id=298 感谢分享!