Home > php教程 > php手册 > body text

一个显示某段时间内每个月的方法 返回由这些月份组成的数组

WBOY
Release: 2016-06-13 12:00:24
Original
1017 people have browsed it

复制代码 代码如下:


/**
* 生成从开始月份到结束月份的月份数组
* 该方法仿照党子皓getDateArr()方法
* @param unknown_type $start
* @param unknown_type $end
*/
function getMonthArr($start, $end)
{
$start = empty($start) ? date('Y-m',strtotime('-1 month')) : $start;
$end = empty($end) ? date('Y-m') : $end;

//转为时间戳
$st = strtotime($start.'-01');
$et = strtotime($end.'-01');

$t = $st;
$i = 0;
while($t {
//这里累加每个月的的总秒数 计算公式:上一月1号的时间戳秒数减去当前月的时间戳秒数
//看不懂自己想去
$d[$i] = trim(date('Y-m',$t),' ');
$t += strtotime('+1 month', $t)-$t;
$i++;
}
return $d;
}

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!