输出每月的第一天和最后一天

<?php

function getDateType($count, $type, $sort = 2)
{
$time = [];
$cyear = floor(date("Y", time()));
$cMonth = floor(date("m", time()));

for ($i = 0; $i < $count; $i++) {

$nMonth = $cMonth - $i;
$cyear = $nMonth == 0 ? ($cyear - 1) : $cyear;
$nMonth = $nMonth <= 0 ? 12 + $nMonth : $nMonth;
$date = $cyear . "-" . $nMonth . "-1";

if ($type == 1) {

$firstday = date('Y-m-01', strtotime($date));
$lastday = date('Y-m-t', strtotime($date));

@$time[$i][0] .= $firstday;
@$time[$i][1] .= $lastday;
} elseif ($type == 2) {

$firstday = date('Y年m月', strtotime($date));
@$time[$i] .= $firstday;
}
}

return $sort != 2 ? $time : array_reverse($time);
}

$a = getDateType(12, 1);

var_dump($a);

 

//输出
array (size=12)
0 => 
array (size=2)
0 => string '2018-07-01' (length=10)
1 => string '2018-07-31' (length=10)
1 => 
array (size=2)
0 => string '2018-08-01' (length=10)
1 => string '2018-08-31' (length=10)
2 => 
array (size=2)
0 => string '2018-09-01' (length=10)
1 => string '2018-09-30' (length=10)
3 => 
array (size=2)
0 => string '2018-10-01' (length=10)
1 => string '2018-10-31' (length=10)
4 => 
array (size=2)
0 => string '2018-11-01' (length=10)
1 => string '2018-11-30' (length=10)
5 => 
array (size=2)
0 => string '2018-12-01' (length=10)
1 => string '2018-12-31' (length=10)
6 => 
array (size=2)
0 => string '2019-01-01' (length=10)
1 => string '2019-01-31' (length=10)
7 => 
array (size=2)
0 => string '2019-02-01' (length=10)
1 => string '2019-02-28' (length=10)
8 => 
array (size=2)
0 => string '2019-03-01' (length=10)
1 => string '2019-03-31' (length=10)
9 => 
array (size=2)
0 => string '2019-04-01' (length=10)
1 => string '2019-04-30' (length=10)
10 => 
array (size=2)
0 => string '2019-05-01' (length=10)
1 => string '2019-05-31' (length=10)
11 => 
array (size=2)
0 => string '2019-06-01' (length=10)
1 => string '2019-06-30' (length=10)

  

posted @ 2019-06-01 09:15  落华  阅读(244)  评论(0编辑  收藏  举报