php获取“今日\昨日\上周\本周\本月\近一月\近三月”的起始时间戳和结束时间戳

/**
 * 获取开始时间和结束时间(时间戳)
 *
 * @author QingLin<[email protected]>
 * DateTime 2023/5/24 16:13
 */
function getSETime($time_type = 0){
    switch ($time_type) {
        case 1:
            // 今天
            $start_time = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
            $end_time   = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
            break;
        case 2:
            // 昨天
            $start_time = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
            $end_time   = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
            break;
        case 3:
            // 本周
            $start_time = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'));
            $end_time   = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'));
            break;
        case 4:
            // 上周
            $start_time = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - 7, date('Y'));
            $end_time   = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7 - 7, date('Y'));
            break;
        case 5:
            // 近一周
            $start_time = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y'));
            $end_time   = TIMESTAMP;
            break;
        case 6:
            // 本月
            $start_time = mktime(0, 0, 0, date('m'), 1, date('Y'));
            $end_time   = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
            break;
        case 7:
            // 上月
            $start_time = mktime(0, 0, 0, date('m')-1, 1, date('Y'));
            $end_time   = mktime(23, 59, 59, date('m'), 0, date('Y'));
            break;
        case 8:
            // 近一月
            $start_time = mktime(0, 0, 0, date('m')-1, date('d'), date('Y'));
            $end_time   = TIMESTAMP;
            break;
        case 9:
            // 近三月
            $start_time = mktime(0, 0, 0, date('m')-3, date('d'), date('Y'));
            $end_time   = TIMESTAMP;
            break;
        case 10:
            // 今年
            $start_time = mktime(0, 0, 0, 1, 1, date('Y'));
            $end_time   = TIMESTAMP;
            break;
        case 11:
            // 近一年
            $start_time = mktime(0, 0, 0, date('m'), date('d'), date('Y')-1);
            $end_time   = TIMESTAMP;
            break;
        default:
            // 自定义时间
            $start_time = input('start_time', '');
            $start_time = !empty($start_time) ? strtotime($start_time) : '';
            $end_time   = input('end_time', '');
            $end_time   = !empty($end_time) ? strtotime($end_time) : '';
    }
    return ['start_time' => $start_time, 'end_time' => $end_time];
}

你可能感兴趣的:(php)