首页
关于小站
朋友
壁纸
留言
时光之书
笔顺字帖
LayUI手册
Search
1
【PHP】PHPoffice/PHPSpreadsheet读取和写入Excel
2,044 阅读
2
【Layui】控制页面元素展示隐藏
1,846 阅读
3
【Git】No tracked branch configured for branch master or the branch doesn't exist.
1,791 阅读
4
【PHP】PHP实现JWT生成和验证
1,733 阅读
5
【composer】composer常用命令
1,584 阅读
默认分类
PHP
ThinkPHP
Laravel
面向对象
设计模式
算法
基础
网络安全
webman
Web
HTML
CSS
JavaScript
jQuery
Layui
VUE
uni-app
Database
MySQL
Redis
RabbitMQ
Nginx
Git
Linux
Soft Ware
Windows
网赚
Go
Docker
Elasticsearch
登录
Search
标签搜索
PHP
函数
方法
类
MySQL
ThinkPHP
JavaScript
OOP
Layui
Web
Server
Docker
PHPSpreadsheet
PHPoffice
Array
设计模式
Nginx
Git
排序算法
基础
小破孩
累计撰写
258
篇文章
累计收到
13
条评论
首页
栏目
默认分类
PHP
ThinkPHP
Laravel
面向对象
设计模式
算法
基础
网络安全
webman
Web
HTML
CSS
JavaScript
jQuery
Layui
VUE
uni-app
Database
MySQL
Redis
RabbitMQ
Nginx
Git
Linux
Soft Ware
Windows
网赚
Go
Docker
Elasticsearch
页面
关于小站
朋友
壁纸
留言
时光之书
笔顺字帖
LayUI手册
搜索到
1
篇与
的结果
2023-04-27
【PHP】针对时间段的处理
/** * 获取最近七天所有日期 */ public function getWeeks($time = '', $format='Y-m-d'){ $time = $time != '' ? $time : time(); //组合数据 $date = []; for ($i=1; $i<=7; $i++){ $date[$i] = date($format ,strtotime( '+' . $i-7 .' days', $time)); } return $date; } /** * 获取起始日期中的所有日期 * @param $start 开始时间 2022-09-22 * @param $end 结束时间 2022-09-29 * @return array */ public function getBetweenTime($startDate, $endDate, $format = 'Y-m-d') { $dates = []; // 确保我们处理的是日期 $start = date('Y-m-d', strtotime($startDate)); $end = date('Y-m-d', strtotime($endDate)); $current = strtotime($start); $last = strtotime($end); while ($current <= $last) { $dates[] = date($format, $current); $current = strtotime('+1 day', $current); } return $dates; } /** * 获取起始时间戳 * @param int|string $type 类型 1本年 2本季度 3上月 4本月 5本周 6上周 7下周 * @param bool $format 是否返回格式化后的时间(Y-m-d H:i:s),默认false返回时间戳 * @return array 包含startTime(开始时间)和endTime(结束时间)的数组 * @throws InvalidArgumentException */ public function getStartEndtime($type = '', bool $format = false): array { // 验证类型参数 $validTypes = [1, 2, 3, 4, 5, 6, 7]; if ($type !== '' && !in_array($type, $validTypes)) { throw new Exception('无效的时间类型参数'); } $now = time(); $startTime = 0; $endTime = 0; switch ($type) { case 1: // 本年 $startTime = strtotime("first day of January " . date('Y', $now) . " 00:00:00"); $endTime = strtotime("last day of December " . date('Y', $now) . " 23:59:59"); break; case 2: // 本季度 $currentMonth = (int)date('n', $now); $currentYear = date('Y', $now); $season = ceil($currentMonth / 3); // 1-4 $seasonStartMonth = ($season - 1) * 3 + 1; $startTime = mktime(0, 0, 0, $seasonStartMonth, 1, $currentYear); $seasonEndMonth = $season * 3; $seasonEndDay = date('t', mktime(0, 0, 0, $seasonEndMonth, 1, $currentYear)); $endTime = mktime(23, 59, 59, $seasonEndMonth, $seasonEndDay, $currentYear); break; case 3: // 上月 $startTime = strtotime("first day of last month 00:00:00"); $endTime = strtotime("last day of last month 23:59:59"); break; case 4: // 本月 $startTime = strtotime("first day of this month 00:00:00"); $endTime = strtotime("last day of this month 23:59:59"); break; case 5: // 本周(周一到周日) $startTime = strtotime('this week Monday 00:00:00'); $endTime = strtotime('this week Sunday 23:59:59'); break; case 6: // 上周 $startTime = strtotime('last week Monday 00:00:00'); $endTime = strtotime('last week Sunday 23:59:59'); break; case 7: // 下周 $startTime = strtotime('next week Monday 00:00:00'); $endTime = strtotime('next week Sunday 23:59:59'); break; default: // 今天 $startTime = strtotime('today 00:00:00'); $endTime = strtotime('today 23:59:59'); break; } // 格式化返回 if ($format) { return [ 'statTime' => date('Y-m-d H:i:s', $startTime), 'endTime' => date('Y-m-d H:i:s', $endTime) ]; } return [ 'startTime' => $startTime, 'endTime' => $endTime ]; } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/11/11 11:34 * @return array * @Description:获取当前时间到一周前的时间 */ public function getTimeStamps() { $todayEnd = strtotime(date('Y-m-d 23:59:59')); $sevenDaysAgoStart = strtotime('-6 days 00:00:00'); return [ 'start' => $sevenDaysAgoStart, 'end' => $todayEnd ]; } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/12/7 11:05 * @param $year * @param $month * @return array * @Description:获取指定月份的开始时间和结束时间 */ public function getMonthStartAndEndTime($year, $month) { $startTime = strtotime($year. '-'. $month. '-01 00:00:00'); $endTime = strtotime($year. '-'. $month. '-'. date('t', strtotime($year. '-'. $month. '-01')). ' 23:59:59'); return ['statTime'=>$startTime,'endTime'=>$endTime]; }
2023年04月27日
407 阅读
0 评论
0 点赞