Home > Backend Development > PHP Tutorial > 一个显示某段时间内每个月的方法 返回由这些月份组成的数组_php技巧

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

WBOY
Release: 2016-05-17 09:11:18
Original
1065 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;
}
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