PHP时间段划分,计算出今天,昨天,本周、上周、本月、上月等时间段

分享一段PHP时间段划分的代码。以今日为基准,计算出今天,昨天,本周、上周、本月、上月等时间段而且各个时间段不重复。

function getOutTimeRanges() {
    $now = time(); // 当前时间戳
    $today = strtotime('today'); // 今天零点时间戳
    $yesterday = strtotime('yesterday'); // 昨天零点时间戳
    $yesterday_start = $yesterday;
    $yesterday_end = $today - 1;
    $this_week_start = strtotime('this week 00:00:00'); // 本周第一天零点时间戳
    // return date('Y-m-d H:i:s',$this_week_start);
    $this_week_end = strtotime('this week +6 days 23:59:59'); // 本周最后一天最后一秒时间戳
    $last_week_start = strtotime('last week 00:00:00'); // 上周第一天零点时间戳
    $last_week_end = strtotime('last week +6 days 23:59:59'); // 上周最后一天最后一秒时间戳
    $this_month_start = strtotime('first day of this month 00:00:00'); // 本月第一天零点时间戳
    $this_month_end = strtotime('last day of this month 23:59:59'); // 本月最后一天最后一秒时间戳
    $last_month_start = strtotime('first day of last month 00:00:00'); // 上月第一天零点时间戳
    $last_month_end = strtotime('last day of last month 23:59:59'); // 上月最后一天最后一秒时间戳

    // 去除交叉部分
    $today_start = strtotime('today 00:00:00');
    $today_end = strtotime('today 23:59:59');

    //今天和本周冲突
    if ($today_start >= $this_week_start && $today_end <= $this_week_end ) {
        if ($today_start == $this_week_start){

            $this_week_start =0; // 本周第一天零点时间戳
            $this_week_end = 0; // 本周最后一天最后一秒时间戳
        }else{
            $this_week_end = strtotime('-1 day', $today_end);
        }

    }
    //昨天和本周冲突
    if ($this_week_start > 0 && $yesterday_start >= $this_week_start && $yesterday_end <= $this_week_end ) {
        if ($yesterday_start == $this_week_start){
            $this_week_start = 0; // 本周第一天零点时间戳
            $this_week_end = 0; // 本周最后一天最后一秒时间戳
        }else{

        $this_week_end = strtotime('-1 day', $yesterday_end);
        }

    }
    //return date('Y-m-d H:i:s',$this_week_end);
    //如果今天和本周是一天
    //交叉月份
    if ($this_week_start == 0){
        if ($today_start >= $this_month_start && $today_end <= $this_month_end) {
            if ($today_start == $this_month_start){
                $this_month_start = 0; // 本周第一天零点时间戳
                $this_month_end = 0; // 本周最后一天最后一秒时间戳
            }

            $this_month_end = strtotime('-1 day', $today_end);

        }
        if ($yesterday_start >= $this_month_start && $yesterday_end <= $this_month_end) {
            if ($yesterday_start == $this_month_start){
                $this_month_start = 0; // 本周第一天零点时间戳
                $this_month_end = 0; // 本周最后一天最后一秒时间戳
            }

            $this_month_end= strtotime('-1 day', $yesterday_end);

        }
    }else{
        if ($this_week_start >= $this_month_start && $this_week_start <= $this_month_end) {
            if ($today_start == $this_month_start){
                $this_month_start = 0; // 本周第一天零点时间戳
                $this_month_end = 0; // 本周最后一天最后一秒时间戳
            }

            $this_month_end = strtotime('-1 day', $this_week_start);

        }
    }

    //上周和昨天
    if ($yesterday_end == $last_week_end ) {

        $last_week_end = strtotime('-1 day', $yesterday_end);

    }
    //上周和本月
    if ($last_week_start > $this_month_start){
        $this_month_end = strtotime('-1 second', $last_week_start);
    }
    //上周和上月
    if ($last_week_start < $last_month_end){
        $last_month_end = strtotime('-1 second', $last_week_start);
    }
    // 格式化时间段
    $yesterday_start = date('Y-m-d H:i:s',$yesterday_start);
    $yesterday_end = date('Y-m-d H:i:s',$yesterday_end);
    $today_start = date('Y-m-d H:i:s',$today_start);
    $today_end = date('Y-m-d H:i:s',$today_end);
    $this_week_start = date('Y-m-d H:i:s',$this_week_start);
    $this_week_end = date('Y-m-d H:i:s',$this_week_end);
    $last_week_start = date('Y-m-d H:i:s',$last_week_start);
    $last_week_end = date('Y-m-d H:i:s',$last_week_end);
    $this_month_start = date('Y-m-d H:i:s',$this_month_start);
    $this_month_end = date('Y-m-d H:i:s',$this_month_end);
    $other_start = '2023-01-01 00:00:00';
    $other_end = date('Y-m-d H:i:s',strtotime('-1 second', $last_month_start));
    $last_month_start = date('Y-m-d H:i:s',$last_month_start);
    $last_month_end = date('Y-m-d H:i:s',$last_month_end);

    // 返回时间段数组
    return array(
        'today' => array('start' => $today_start, 'end' => $today_end),
        'yesterday' => array('start' => $yesterday_start, 'end' => $yesterday_end),
        'this_week' => array('start' => $this_week_start, 'end' => $this_week_end),
        'last_week' => array('start' => $last_week_start, 'end' => $last_week_end),
        'this_month' => array('start' => $this_month_start, 'end' => $this_month_end),
        'last_month' => array('start' => $last_month_start, 'end' => $last_month_end),
        'other'=>array('start'=>$other_start,'end'=>$other_end),
    );
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值