Home > Backend Development > PHP Tutorial > php获取某段时间内每个月的方法

php获取某段时间内每个月的方法

WBOY
Release: 2016-06-20 13:03:46
Original
1827 people have browsed it

php获取某段时间内每个月的方法,返回由这些月份组成的数组,具体代码如下:

<p>/** </p>* 生成从开始月份到结束月份的月份数组<br />* @param int $start 开始时间戳<br />* @param int $end 结束时间戳<br />*/ <br />function monthList($start,$end){<br />	if(!is_numeric($start)||!is_numeric($end)||($end<=$start)) return '';<br />	$start=date('Y-m',$start);<br />	$end=date('Y-m',$end);<br />	//转为时间戳<br />	$start=strtotime($start.'-01');<br />	$end=strtotime($end.'-01');<br />	$i=0;//http://www.scutephp.com/<br />	$d=array();<br />	while($start<=$end){<br />		//这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数<br />		$d[$i]=trim(date('Y-m',$start),' ');<br />		$start+=strtotime('+1 month',$start)-$start;<br />		$i++;<br />	} <br />	return $d;<br /><p>}</p>
Copy after login

例如:

echo '<pre class="brush:php;toolbar:false">';print_r(monthList(1395283229,1398960000));
Copy after login

结果将得到如下:

Array

(
[0] => 2014-03
[1] => 2014-04
[2] => 2014-05
)


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template