这篇关于php上个月最后一天的文章,主要介绍php获取某段时间内每个月的方法,返回由这些月份组成的数组,觉得挺不错的,感兴趣的php开发者可以参考下,希望对大家在学习php的成长路上有所帮助!
php获取某段时间内每个月的方法,返回由这些月份组成的数组,具体代码如下:
/**
* 生成从开始月份到结束月份的月份数组
* @param int $start 开始时间戳
* @param int $end 结束时间戳
*/
function monthList($start,$end){
if(!is_numeric($start)||!is_numeric($end)||($end<=$start)) return '';
$start=date('Y-m',$start);
$end=date('Y-m',$end);
//转为时间戳
$start=strtotime($start.'-01');
$end=strtotime($end.'-01');
$i=0;//http://www.feishuai.vip/php-function/224.html
$d=array();
while($start<=$end){
//这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数
$d[$i]=trim(date('Y-m',$start),' ');
$start+=strtotime('+1 month',$start)-$start;
$i++;
}
return $d;
}
例如:
echo '<pre>';print_r(monthList(1395283229,1398960000));
结果将得到如下:
Array ( [0] => 2014-03 [1] => 2014-04 [2] => 2014-05 )
总结
以上就是关于php上个月最后一天全部内容,希望这篇php获取某段时间内每个月的方法,返回由这些月份组成的数组文章能够帮你解决如相关的PHP问题,更多请关注PHP栏目的其它相关文章!