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

<?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)

  

转载于:https://www.cnblogs.com/qczy/p/10958495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值