PHP生成一段时间之间的月份列表
今天项目中需要生成一段时间的月份列表,自己随便写了个,以后再继续优化了。使用时可根据自己需要进行修改。
function getMonthList ($timestamp1, $timestamp2)
{
$yearsyn = date('Y', $timestamp1);
$monthsyn = date('m', $timestamp1);
$daysyn = date('d', $timestamp1);
$yearnow = date('Y', $timestamp2);
$monthnow = date('m', $timestamp2);
$daynow = date('d', $timestamp2);
if ($yearsyn == $yearnow)
{
$monthinterval = $monthnow - $monthsyn;
}
else if ($yearsyn < $yearnow)
{
$yearinterval = $yearnow - $yearsyn -1;
$monthinterval = (12 - $monthsyn + $monthnow) + 12 * $yearinterval;
}
$timedata = array();
for ($i = 0; $i <= $monthinterval; $i++)
{
$tmptime = mktime(0, 0, 0, $monthsyn + $i, 1, $yearsyn);
$timedata[$i]['year'] = date('Y', $tmptime);
$timedata[$i]['month'] = date('m', $tmptime);
$timedata[$i]['beginday'] = '01';
$timedata[$i]['endday'] = date('t', $tmptime);
}
$timedata[0]['beginday'] = $daysyn;
$timedata[$monthinterval]['endday'] = $daynow;
unset($tmptime);
return $timedata;
}
{
$yearsyn = date('Y', $timestamp1);
$monthsyn = date('m', $timestamp1);
$daysyn = date('d', $timestamp1);
$yearnow = date('Y', $timestamp2);
$monthnow = date('m', $timestamp2);
$daynow = date('d', $timestamp2);
if ($yearsyn == $yearnow)
{
$monthinterval = $monthnow - $monthsyn;
}
else if ($yearsyn < $yearnow)
{
$yearinterval = $yearnow - $yearsyn -1;
$monthinterval = (12 - $monthsyn + $monthnow) + 12 * $yearinterval;
}
$timedata = array();
for ($i = 0; $i <= $monthinterval; $i++)
{
$tmptime = mktime(0, 0, 0, $monthsyn + $i, 1, $yearsyn);
$timedata[$i]['year'] = date('Y', $tmptime);
$timedata[$i]['month'] = date('m', $tmptime);
$timedata[$i]['beginday'] = '01';
$timedata[$i]['endday'] = date('t', $tmptime);
}
$timedata[0]['beginday'] = $daysyn;
$timedata[$monthinterval]['endday'] = $daynow;
unset($tmptime);
return $timedata;
}