php中获取本周本月本年所有时间以及时间戳

36 篇文章 0 订阅
基本所有的数据统计都离不开基础条件-时间数据
封装了一个function方便调用(可以根据自己想要的格式进行更改,支持指定时间输出)
	/**
     * @name: 获取本周本月本年 - 时间数据
     * @param {int} $time_frame 时间类型 1本周 2本月 3本年 4指定时间范围
     * @param {int} $start_time 指定时间-开始
     * @param {int} $end_time   指定时间-结束
     * @return [
        date_name 			时间显示名称
        date 				相对标准时间
        start_time 			开始时间戳
        end_time			结束时间戳[时间戳用于where条件搜索]
        is_today_or_month	是否当天/月[1是 2否]
       ]
     * @author: Turbo
     * @Date: 2021-06-01 16:46:03
     */       
    private function get_time_data($time_frame = 1, $start_time = '', $end_time = '')
    {
        $time = time();

        if ($time_frame == 1) {
            // 本周
            $week = date('w', $time); // 当前周几
            $day = [];
            for ($i = 1; $i <= 7; $i++){
                $day[$i-1]['date_name'] = date('m.d' ,strtotime( '+'.$i-$week.' days', $time)); // 日期显示格式
                $day[$i-1]['date'] = date('Y-m-d' ,strtotime( '+'.$i-$week.' days', $time));
                $day[$i-1]['start_time'] = strtotime($day[$i-1]['date'].' 00:00:00');
                $day[$i-1]['end_time'] = strtotime($day[$i-1]['date'].' 23:59:59');
                // 判断是否当天
                if (date('Y-m-d' ,strtotime( '+'.$i-$week.' days', $time)) == date('Y-m-d',$time)) {
                    $day[$i-1]['is_today_or_month'] = 1; // 是
                }else{
                    $day[$i-1]['is_today_or_month'] = 2; // 否
                }
            }
            $time_data = $day;
        }elseif ($time_frame == 2) {
            // 本月
            $month = date('d', $time); // 当前月份
            $week = [];
            for ($i = 1; $i <= date('t', $time); $i++){
                $week[$i-1]['date_name'] = date('d', strtotime( '+'. $i-$month .' days', $time)); // 日期显示格式
                $week[$i-1]['date'] = date('Y-m-d', strtotime( '+'. $i-$month .' days', $time));
                $week[$i-1]['start_time'] = strtotime($week[$i-1]['date'].' 00:00:00');
                $week[$i-1]['end_time'] = strtotime($week[$i-1]['date'].' 23:59:59');
                // 判断是否当天
                if (date('Y-m-d', strtotime( '+'. $i-$month .' days', $time)) == date('Y-m-d',$time)) {
                    $week[$i-1]['is_today_or_month'] = 1;
                }else{
                    $week[$i-1]['is_today_or_month'] = 2;
                }
            }
            $time_data = $week;
        }elseif ($time_frame == 3) {
            // 本年
            $year = date('Y', $time); // 当前年份
            $month = [];
            for ($i = 1; $i <= 12 ; $i++) {
                $month[$i-1]['date_name'] = $i; // 日期显示格式
                $month[$i-1]['date'] = $year.'-'.$i;
                $month[$i-1]['start_time'] = strtotime(date( 'Y-m-d 00:00:00', strtotime($month[$i-1]['date']) ));
                $month[$i-1]['end_time'] = strtotime(date( 'Y-m-' . date( 't',strtotime($month[$i-1]['date'])) . ' 23:59:59', strtotime($month[$i-1]['date']) ));
                // 判断是否当月
                if (($year.'-'.$i) == date('Y-n',$time)) {
                    $month[$i-1]['is_today_or_month'] = 1;
                }else{
                    $month[$i-1]['is_today_or_month'] = 2;
                }
            }
            $time_data = $month;
        }elseif ($time_frame == 4) {
            // 指定时间范围
            $time_data = [];
            $i = 0;
            while ($start_time <= $end_time){
                $time_data[$i]['date_name'] = date('Y/n/j',$start_time); // 日期显示格式
                $time_data[$i]['start_time'] = strtotime(date('Y-m-d 00:00:00',$start_time));
                $time_data[$i]['end_time'] = strtotime(date('Y-m-d 23:59:59',$start_time));
                // 判断是否当天
                if (date('Y-m-d',$start_time) == date('Y-m-d',$time)) {
                    $time_data[$i]['is_today_or_month'] = 1;
                }else{
                    $time_data[$i]['is_today_or_month'] = 2;
                }
                $start_time = strtotime('+1 day',$start_time);
                $i++;
            }
        }
        return $time_data;
    }
进行调用
$time_data = $this->get_time_data($time_frame);
var_dump($time_data);
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值