龙空技术网

PHP获取月的每周开始结束时间以及年的开始结束时间

PHP程序员小王 159

前言:

而今朋友们对“php今天的时间戳”可能比较注重,朋友们都需要分析一些“php今天的时间戳”的相关资讯。那么小编也在网络上搜集了一些有关“php今天的时间戳””的相关内容,希望兄弟们能喜欢,我们一起来学习一下吧!

日期函数基本使用

$a = "2022-12-10 10:00:00";print_r($a);        echo PHP_EOL; // 2022-12-10 10:00:00print_r(date('Y-m-d H:i:s',strtotime("$a + 1 days")));        echo PHP_EOL; // 2022-12-11 10:00:00print_r(date('Y-m-d H:i:s',strtotime("$a + 2 days")));        echo PHP_EOL; // 2022-12-12 10:00:00print_r(date('Y-m-d H:i:s',strtotime("$a + 3 days")));        echo PHP_EOL; // 2022-12-13 10:00:00print_r(date('Y-m-d H:i:s',strtotime("$a + 1 weeks")));       echo PHP_EOL; // 2022-12-17 10:00:00print_r(date('Y-m-d H:i:s',strtotime("$a + 2 weeks")));       echo PHP_EOL; // 2022-12-24 10:00:00print_r(date('Y-m-d H:i:s',strtotime("$a + 1 weeks 2 days - 4 hours + 1 minutes + 2 seconds")));       echo PHP_EOL; // 2022-12-19 06:01:02print_r(date('Y-m-d H:i:s',strtotime("$a next Thursday")));       echo PHP_EOL; // 2022-12-15 00:00:00print_r(date('Y-m-d H:i:s',strtotime("$a last Monday")));       echo PHP_EOL; // 2022-12-05 00:00:00print_r(date('Y-m-d H:i:s',strtotime("$a + 1 months")));       echo PHP_EOL; // 2023-01-10 10:00:00// 季度 --> 当前月所属季度的开始结束日期 now 2022-12-26 10:30:00$quarter = ceil((date('m'))/3);$time_amp = [  'start' => date('Y-m-d H:i:s',mktime(0, 0, 0,$quarter*3-2,1,date('Y'))),// 2022-10-01 00:00:00  'over_1' => date('Y-m-d H:i:s',mktime(0, 0, 0,$quarter*3+1,1,date('Y'))),// 2023-01-01 00:00:00  'over_2' => date('Y-m-d H:i:s',mktime(0, 0, 0,$quarter*3+1,1,date('Y'))-1),// 2022-12-31 23:59:59];print_r($time_amp);/*         * 年year 月month 日day 时hour 分minute 秒second 周week         * 星期一:Mon.=Monday 星期二:Tues.=Tuesday 星期三:Wed.=Wednesday 星期四:Thur.=Thursday 星期五:Fri.=Friday 星期六:Sat.=Saturday 星期天:Sun.=Sunday         * 一 月:January 简写: Jan  二 月:February 简写:Feb     三 月:March 简写:Mar         * 四 月:April 简写:Apr     五 月:May 简写:May          六 月:June 简写:Jun         * 七 月:July 简写:Jul      八 月:August 简写:Aug       九 月:September 简写:Sep         * 十 月:October 简写:Oct   十一月:November 简写:Nov    十二月:December 简写:Dec         */

某一月从第一周到最后一周

$month = '2022-11';$year_start = date("Y-m-01",strtotime($month));$year_end = date("Y-m",strtotime($month)).'-'.date('t',strtotime($month));print_r(compact('year_start','year_end')); // 当月第一天 当月最后一天$start_day = strtotime($year_start);// 如果当月第一天是不是周一获取下一个周一的时间戳if (intval(date('N', $start_day)) != '1') {  $start_day = strtotime("next monday", strtotime($year_start));}$year_mon_dy = date("Y-m-d", $start_day);$end_day = strtotime($year_end);// 如果当月最后一天不是周日获取下一个周日的时间戳if (intval(date('N', $end_day)) != '7') {  $end_day = strtotime("next sunday", strtotime($year_end));}$year_sun_dy = date('Y-m-d',$end_day);$day_diff = (((strtotime($year_sun_dy) - strtotime($year_mon_dy))/86400) + 1) / 7;$start_ymd = [];$sql = [];for ($i=0;$i<$day_diff;$i++){  $value = date('Y-m-d',strtotime($year_mon_dy) + $i * 86400 * 7 );  $start_ymd[$i+1] = $value;  $sql[$i+1] = Db::name('example')->whereWeek('create_time',$value)->fetchSql(true)->find(); // 框架tp6 create_time 字段 datetime格式}print_r($start_ymd);print_r($sql);

运行结果

Array(    [year_start] => 2022-11-01    [year_end] => 2022-11-30)Array(    [1] => 2022-11-07    [2] => 2022-11-14    [3] => 2022-11-21    [4] => 2022-11-28)Array(    [1] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-11-07 00:00:00' AND '2022-11-13 23:59:59' LIMIT 1    [2] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-11-14 00:00:00' AND '2022-11-20 23:59:59' LIMIT 1    [3] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-11-21 00:00:00' AND '2022-11-27 23:59:59' LIMIT 1    [4] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-11-28 00:00:00' AND '2022-12-04 23:59:59' LIMIT 1)

某一年从第一周到最后一周

// 年周$year = '2023';$year_start = $year . "-01-01";$year_end = $year . "-12-31";$start_day = strtotime($year_start);// 如果当年第一天是不是周一获取下一个周一的时间戳if (intval(date('N', $start_day)) != '1') {  $start_day = strtotime("next monday", strtotime($year_start)); //获取年第一周的日期}$year_mon_dy = date("Y-m-d", $start_day); //获取年第一周的日期$end_day = strtotime($year_end);// 如果当年最后一天不是周日获取下一个周日的时间戳if (intval(date('N', $end_day)) != '7') {  $end_day = strtotime("next sunday", strtotime($year_end));}// 获取年最后一个周日是第几周 52$num = intval(date('W', $end_day));$week_array = [];$sql = [];for ($i = 1; $i <= $num; $i++) {  $j = $i -1;  $start_date = date("Y-m-d", strtotime("$year_mon_dy $j week "));  $end_date = date("Y-m-d", strtotime("$start_date +6 day"));  $week_array[$i] = [$start_date,$end_date];  $sql[$i] = Db::name('example')->whereWeek('create_time',$start_date)->fetchSql(true)->find(); // 框架tp6 create_time 字段 datetime格式}print_r($week_array);print_r($sql);

运行结果

Array(    [1] => Array        (            [0] => 2023-01-02            [1] => 2023-01-08        )    [2] => Array        (            [0] => 2023-01-09            [1] => 2023-01-15        )    [3] => Array        (            [0] => 2023-01-16            [1] => 2023-01-22        ) ........................................................................................................................)Array(    [1] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2023-01-02 00:00:00' AND '2023-01-08 23:59:59' LIMIT 1    [2] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2023-01-09 00:00:00' AND '2023-01-15 23:59:59' LIMIT 1    [3] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2023-01-16 00:00:00' AND '2023-01-22 23:59:59' LIMIT 1    [4] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2023-01-23 00:00:00' AND '2023-01-29 23:59:59' LIMIT 1    [5] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2023-01-30 00:00:00' AND '2023-02-05 23:59:59' LIMIT 1    ........................................................................................................................)

洗出来当月上月

// 传入年份获取月$year = '2022';$res = [];if ($year == date('Y',time())){  $year_month = date('Y-m',time());  $now_month_num = date('n',strtotime($year_month));  for ($i=1;$i<=$now_month_num;$i++){    // 判断当前年展示当月上月    if ($i == $now_month_num){$name = '当月';}elseif ($i == $now_month_num-1){$name = '上月';}else{$name = $i.'月';}    $value = $year.'-'.date('m',strtotime($year.'-'.$i));    $start_over = [      'start' => $year.'-'.date('m-d 00:00:00',strtotime($value)),        'over' => $year.'-'.date('m-d H:i:s',strtotime("$value + 1 months - 1 seconds")),          ];    $sql = Db::name('example')->whereMonth('create_time',$value)->fetchSql(true)->find(); // 框架tp6 create_time 字段 datetime格式    $item = compact('name','value','start_over','sql');    array_unshift($res,$item);  }}else{  for ($i=1;$i<=12;$i++){    $name = $i.'月';    $value = $year.'-'.date('m',strtotime($year.'-'.$i));    $start_over = [      'start' => $year.'-'.date('m-d 00:00:00',strtotime($value)),        'over' => $year.'-'.date('m-d H:i:s',strtotime("$value + 1 months - 1 seconds")),          ];    $sql = Db::name('example')->whereMonth('create_time',$value)->fetchSql(true)->find(); // 框架tp6 create_time 字段 datetime格式    $item = compact('name','value','start_over','sql');    array_unshift($res,$item);  }}print_r($res);

运行结果

Array(    [0] => Array        (            [name] => 当月            [value] => 2022-12            [start_over] => Array                (                    [start] => 2022-12-01 00:00:00                    [over] => 2022-12-31 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-12-01 00:00:00' AND '2022-12-31 23:59:59' LIMIT 1        )    [1] => Array        (            [name] => 上月            [value] => 2022-11            [start_over] => Array                (                    [start] => 2022-11-01 00:00:00                    [over] => 2022-11-30 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-11-01 00:00:00' AND '2022-11-30 23:59:59' LIMIT 1        )    [2] => Array        (            [name] => 10月            [value] => 2022-10            [start_over] => Array                (                    [start] => 2022-10-01 00:00:00                    [over] => 2022-10-31 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-10-01 00:00:00' AND '2022-10-31 23:59:59' LIMIT 1        )    [3] => Array        (            [name] => 9月            [value] => 2022-09            [start_over] => Array                (                    [start] => 2022-09-01 00:00:00                    [over] => 2022-09-30 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-09-01 00:00:00' AND '2022-09-30 23:59:59' LIMIT 1        )........................................................................................................................    [11] => Array        (            [name] => 1月            [value] => 2022-01            [start_over] => Array                (                    [start] => 2022-01-01 00:00:00                    [over] => 2022-01-31 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-01-01 00:00:00' AND '2022-01-31 23:59:59' LIMIT 1        ))

如果传入2021年效果如下

Array(    [0] => Array        (            [name] => 12月            [value] => 2021-12            [start_over] => Array                (                    [start] => 2021-12-01 00:00:00                    [over] => 2021-12-31 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2021-12-01 00:00:00' AND '2021-12-31 23:59:59' LIMIT 1        )    [1] => Array        (            [name] => 11月            [value] => 2021-11            [start_over] => Array                (                    [start] => 2021-11-01 00:00:00                    [over] => 2021-11-30 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2021-11-01 00:00:00' AND '2021-11-30 23:59:59' LIMIT 1        )........................................................................................................................    [10] => Array        (            [name] => 2月            [value] => 2021-02            [start_over] => Array                (                    [start] => 2021-02-01 00:00:00                    [over] => 2021-02-28 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2021-02-01 00:00:00' AND '2021-02-28 23:59:59' LIMIT 1        )    [11] => Array        (            [name] => 1月            [value] => 2021-01            [start_over] => Array                (                    [start] => 2021-01-01 00:00:00                    [over] => 2021-01-31 23:59:59                )            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2021-01-01 00:00:00' AND '2021-01-31 23:59:59' LIMIT 1        ))

xxxx年到xxx年 查询统计区间

// 年$start = '2019';$over = date('Y');$diff_arr = date_diff(date_create($over.'-01-01 00:00:00'),date_create($start.'-01-01 00:00:00'));$diff = $diff_arr->format('%y');$res = [];for ($i=0;$i<$diff;$i++){  $year = intval($start) + $i;  $start = $year.'-'.'01-01 00:00:00';  $over = date('Y-m-d H:i:s',strtotime("$start + 1 years - 1 seconds"));  $sql = Db::name('example')->whereYear('create_time',$year)->fetchSql(true)->find(); // 框架tp6 create_time 字段 datetime格式  array_push($res,compact('year','start','over','sql'));}print_r($res);

运行结果

Array(    [0] => Array        (            [year] => 2019            [start] => 2019-01-01 00:00:00            [over] => 2019-12-31 23:59:59            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2019-01-01 00:00:00' AND '2019-12-31 23:59:59' LIMIT 1        )    [1] => Array        (            [year] => 2020            [start] => 2020-01-01 00:00:00            [over] => 2020-12-31 23:59:59            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2020-01-01 00:00:00' AND '2020-12-31 23:59:59' LIMIT 1        )    [2] => Array        (            [year] => 2022            [start] => 2022-01-01 00:00:00            [over] => 2022-12-31 23:59:59            [sql] => SELECT * FROM `uu_example` WHERE  `create_time` BETWEEN '2022-01-01 00:00:00' AND '2022-12-31 23:59:59' LIMIT 1        ))

标签: #php今天的时间戳