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.phpernote.com/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
)
打赏

本文固定链接: https://www.cxy163.net/archives/3850 | 绝缘体

该日志由 绝缘体.. 于 2014年03月18日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: php获取某段时间内每个月的方法,返回由这些月份组成的数组 | 绝缘体
关键字: , , , ,

php获取某段时间内每个月的方法,返回由这些月份组成的数组:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter