[TOC] #### 前言 --- 框架內(nèi)置了常用的時(shí)間查詢方法,參考官方文檔:[https://www.kancloud.cn/manual/thinkphp6_0/1037565](https://www.kancloud.cn/manual/thinkphp6_0/1037565) #### 1、查詢今天,昨天、某天的數(shù)據(jù) --- **查詢今天的數(shù)據(jù),下面兩種查詢方法等價(jià)** ``` whereDay('create_time') whereTime('create_time', 'today') ``` **查詢昨天的數(shù)據(jù)** ``` whereDay('create_time', 'yesterday') ``` #### 2、查詢本周數(shù)據(jù) --- 查詢方法 ``` whereWeek('create_time') ``` #### 3、查詢今天、本周、本月、本季度、本年數(shù)據(jù)(搜索器) --- ``` /** * 根據(jù)時(shí)間篩選數(shù)據(jù) */ public function searchTimeAttr($query, $value, $data) { switch ($value) { case 'today': //今日 $query->whereDay('create_time'); break; case 'week': //本周 $query->whereWeek('create_time'); break; case 'month': //本月 $query->whereMonth('create_time'); break; case 'year': //本年 $query->whereYear('create_time'); break; case 'quarter': //本季度 $query->whereBetweenTime('create_time', ...getQuarterTime()); break; default: break; } } ``` **在 common.php 中定義getQuarterTime() 函數(shù)** ``` /** * 獲取當(dāng)前季度時(shí)間范圍 */ function getQuarterTime() { $month = intval(date('m')); if ($month < 4) { $start = date('Y-01-01 00:00:00'); $end = date("Y-03-31 23:59:59"); } else if ($month < 7) { $start = date('Y-04-01 00:00:00'); $end = date("Y-06-30 23:59:59"); } else if ($month < 10) { $start = date('Y-07-01 00:00:00'); $end = date("Y-09-30 23:59:59"); } else { $start = date('Y-10-01 00:00:00'); $end = date("Y-12-31 23:59:59"); } return [$start, $end]; } ``` #### 4、常用時(shí)間相關(guān)操作 --- **明天某個(gè)時(shí)間的日期** ``` date('Y-m-d', strtotime('+1 days')) . ' 07:00:00' ``` **今天時(shí)間戳范圍** ``` strtotime(date('Y-m-d')) strtotime(date('Y-m-d', strtotime('+1 days'))) - 1 ```