php上月下月时间获取

本文介绍了一种使用PHP进行日期操作的方法,包括获取前一个月和后一个月的起始及结束日期。通过简单的函数调用即可实现这些功能,适用于财务统计等场景。

这个在后台做统计查询的时候用的到,这次是财务妹子让我加的,其实也就是利用php自带的函数就能实现了,感觉不错:

           if($pre_month && $pre_month == 1 && $start_date && $end_date ){
           $timestamp=strtotime($start_date);
           $start_date=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)-1).'-01'));
           $end_date=date('Y-m-d',strtotime("$start_date +1 month -1 day"));

     }

查询下个月时间:

if($next_month && $next_month == 1 && $start_date && $end_date ){
         $time_arr =   $this->getNextMonthDays($start_date);
           $start_date=$time_arr[0];
           $end_date=$time_arr[1];

       }




private  function getNextMonthDays($date){
    $timestamp=strtotime($date);
    $arr=getdate($timestamp);
    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',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));
        $lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
    }
    return array($firstday,$lastday);
}

复制进去直接就能用,这样感觉果然爽多了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值