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

计算过去N月每个自然月的起止时间戳

WBOY
Release: 2016-06-06 19:38:15
Original
1106 people have browsed it

适用于需要按自然月度分析数据的场合 无 /** * 计算过去N月每个自然月的起止时间戳,包括当前月份 * @param integer $max 月份数,默认为6 * @param string $monthstr 返回值数组的第三个下标(具体月份的表示形式) * @return array 每个子数组形式array(月起

适用于需要按自然月度分析数据的场合
/**
 * 计算过去N月每个自然月的起止时间戳,包括当前月份
 * @param  integer $max      月份数,默认为6
 * @param  string  $monthstr 返回值数组的第三个下标(具体月份的表示形式)
 * @return array            每个子数组形式array(月起始时间戳,月结束时间戳,月份名称)
 */
function month_offset($max= 6,$monthstr = 'ym'){
	if ($max<=0) {
		return false;
	}
	$base = date('Y-m-01');
	$y = 1;
	$mo = array();
	$mo[0][0] = strtotime($base);
	$mo[0][1] = time();
	$mo[0][2] = date('ym');
	while ($y < $max) {
		$mo[$y][0] = strtotime(date('Y-m-01',strtotime($base.' -'.$y.' month')));
		$mo[$y][1] = $y == 1?$mo[0][0]:$mo[$y-1][0];
		$mo[$y][2] = date($monthstr,$mo[$y][0]);
		$y++;
	}
	return array_reverse($mo);
}

var_export(month_offset(3));
exit;
/*array (
  0 => 
  array (
    0 => 1425139200,
    1 => 1427817600,
    2 => '1503',
  ),
  1 => 
  array (
    0 => 1427817600,
    1 => 1430409600,
    2 => '1504',
  ),
  2 => 
  array (
    0 => 1430409600,
    1 => 1430702254,
    2 => '1505',
  ),
)[Finished in 0.1s]*/
Copy after login
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template