编程语言
首页 > 编程语言> > PHP获取上周一和上个月

PHP获取上周一和上个月

作者:互联网

PHP获取上周一有个坑,如果今天是周一,获取的是上周一。如果今天是周二到周日,获取的是本周一。

根据传递的页码数和每页显示多少条,获取对应的数据:

     if ($data['type'] == 'day') {
            $unit = 'day';
        } else if ($data['type'] == 'week') {
            $unit = 'monday';
        } else if ($data['type'] == 'month') {
            $unit = 'month';
        }

        $time = time();
        $today = date("Y-m-d", $time);
        $end_offset = ($data['page'] - 1) * $data['size'] + 1;
        $start_offset = $end_offset + $data['size'] - 1;

        $start_timestamp = strtotime("{$today} -{$start_offset} $unit");
        $end_timestamp = strtotime("{$today} -{$end_offset} $unit");
        if ($data['type'] == 'week') {
            $end_timestamp += 86400 * 7 - 1;
            if (date("w", $time) != 1) {
                $start_timestamp -= 86400 * 7;
                $end_timestamp -= 86400 * 7;
            }
        } else if ($data['type'] == 'month') {
            $start_timestamp = strtotime(date("Y-m-01 00:00:00", $start_timestamp));
            $end_timestamp = strtotime(date("Y-m-t 23:59:59", $end_timestamp));
        }
     $flag = true;
        $stamp = strtotime(date("Y-m-d 00:00:00", $end_timestamp));
        while ($flag) {
            if (isset($array_map[$stamp]) && !empty($array_map[$stamp])) {
                $result[] = $array_map[$stamp];
            } else {
                $default['stamps'] = $stamp;
                $default['date'] = date("Ymd", $stamp);
                $result[] = $default;
            }

            if ($data['type'] == 'day') {
                $stamp -= 86400;
            } else if ($data['type'] == 'week') {
                $stamp -= 86400 * 7;
            } else if ($data['type'] == 'month') {
                //$stamp对应的月份的1号,往前1天就是上个月的最后一天
                $date = date("Y-m-01", $stamp);
                $stamp = strtotime("{$date} -1 day");
            }

            if ($stamp < $start_timestamp) {
                $flag = false;
            }
        }

 

标签:end,stamp,timestamp,获取,type,上个月,date,PHP,data
来源: https://www.cnblogs.com/rxbook/p/10937768.html