php 获取时间

3 篇文章 0 订阅
/**
 * 获取指定时间的本月起止时间
 * @param $time int
 * @return mixed 11
 */
function currentMonth($time)
{
    $info['start'] = strtotime(date( 'Y-m-1 00:00:00', $time ));
    $mdays         = date( 't', $time );
    $info['end']   = strtotime(date( 'Y-m-' . $mdays . ' 23:59:59', $time ));
    return $info;
}

/**
 * 获取指定时间的上个月起止时间
 * @param $time int
 * @return mixed 11
 */
function lastMonth($time)
{
    $datetime      = date('Ymd',$time);
    $info['start'] = strtotime(date("Y-m-1 00:00:00",strtotime("last months",strtotime($datetime))));
    $mdays         = date( 't', $time );
    $info['end']   = strtotime(date( 'Y-m-' . $mdays . ' 23:59:59', $info['start']));
    return $info;
}

/**
 * 获取指定时间下个月的起止时间(Y-m-d)
 * @param $time /int
 * @return array
 */
function nextMonth($time){
    $arr=getdate($time);
    if($arr['mon'] == 12){
        $year=$arr['year'] +1;
        $month=$arr['mon'] -11;
        $firstday=$year.'-0'.$month.'-01';
        $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
    }else{
        $firstday=date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)+1).'-01'));
        $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
    }
    $info['start'] = $firstday;
    $info['end']   = $lastday;
    return $info;
}

/**
 * 获取指定时间上一年对应月份的起止时间
 * @param $time
 * @return mixed
 */
function lastYearMonth($time)
{
    $info['start'] = strtotime(date( 'Y-m-01 00:00:00', strtotime("-1 years",$time) ));
    $mdays         = date( 't', $time );
    $info['end']   = strtotime(date( 'Y-m-' . $mdays . ' 23:59:59',  strtotime("-1 years",$time) ));
    return $info;
}

/**
 * 获取指定时间的当年起止时间
 * @param $time int
 * @return mixed 11
 */
function currentYear($time)
{
    $info['start'] = strtotime(date( 'Y-01-01 00:00:00', $time ));
    $info['end']   = strtotime(date( 'Y-12-31 23:59:59', $time ));
    return $info;
}

/**
 * 获取指定时间的上一年起止时间
 * @param $time int
 * @return mixed 11
 */
function lastYear($time)
{
    $info['start'] = strtotime(date( 'Y-01-01 00:00:00', strtotime("-1 years",$time) ));
    $info['end']   = strtotime(date( 'Y-12-31 23:59:59', strtotime("-1 years",$time) ));
    return $info;
}

/**
 * 输入起止时间查询所有月份("Y-m"格式)
 * @param $params
 * @param int $k
 * @return array
 */
function allMonth($params,$k=0)
{
    //先算月份,再算开始时间的月份开始/结束时间,再算结束时间的月份开始结束时间
    $mouth  = array();
    $start  = (date( 'Y-m', $params['startTime'] ));
    $end    = (date( 'Y-m', $params['endTime']));
    if($start <= $end){
        $mouth[$k] = $end;
        $params['endTime'] = strtotime(date( 'Y-m', strtotime("last months",$params['endTime'])));
        $mouth = array_merge($mouth,allMonth($params,$k+1));
    }
    return $mouth;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值