- 工信部備案號 滇ICP備05000110號-1
- 滇公安備案 滇53010302000111
- 增值電信業務經營許可證 B1.B2-20181647、滇B1.B2-20190004
- 云南互聯網協會理事單位
- 安全聯盟認證網站身份V標記
- 域名注冊服務機構許可:滇D3-20230001
- 代理域名注冊服務機構:新網數碼
時間比較
使用where方法
where方法支持時間比較,例如:
// 大于某個時間
where('create_time','>','2019-1-1');
// 小于某個時間
where('create_time','<=','2019-1-1');
// 時間區間查詢
where('create_time','between',['2018-1-1','2019-1-1']);
第三個參數可以傳入任何有效的時間表達式,會自動識別你的時間字段類型,支持的時間類型包括timestamps、datetime、date和int。
使用whereTime方法
whereTime方法提供了日期和時間字段的快捷查詢,示例如下:
// 大于某個時間
Db::table('think_user')->whereTime('birthday', '>=', '1970-10-1')->select();
// 小于某個時間
Db::table('think_user')->whereTime('birthday', '<', '2000-10-1')->select();
// 時間區間查詢
Db::table('think_user')->whereTime('birthday', 'between', ['1970-10-1', '2000-10-1'])->select();
// 不在某個時間區間
Db::table('think_user')->whereTime('birthday', 'not between', ['1970-10-1', '2000-10-1'])->select();
時間表達式
還提供了更方便的時間表達式查詢,例如:
// 獲取今天的博客
Db::table('think_blog') ->whereTime('create_time', 'today')->select();
// 獲取昨天的博客
Db::table('think_blog')->whereTime('create_time', 'yesterday')->select();
// 獲取本周的博客
Db::table('think_blog')->whereTime('create_time', 'week')->select();
// 獲取上周的博客
Db::table('think_blog')->whereTime('create_time', 'last week')->select();
// 獲取本月的博客
Db::table('think_blog')->whereTime('create_time', 'month')->select();
// 獲取上月的博客
Db::table('think_blog')->whereTime('create_time', 'last month')->select();
// 獲取今年的博客
Db::table('think_blog')->whereTime('create_time', 'year')->select();
// 獲取去年的博客
Db::table('think_blog')->whereTime('create_time', 'last year')->select();
如果查詢當天、本周、本月和今年的時間,還可以簡化為:
// 獲取今天的博客
Db::table('think_blog')->whereTime('create_time', 'd')->select();
// 獲取本周的博客
Db::table('think_blog')->whereTime('create_time', 'w')->select();
// 獲取本月的博客
Db::table('think_blog')->whereTime('create_time', 'm')->select();
// 獲取今年的博客
Db::table('think_blog')->whereTime('create_time', 'y') ->select();
V5.0.5+版本開始,還可以使用下面的方式進行時間查詢
// 查詢兩個小時內的博客Db::table('think_blog')->whereTime('create_time','-2 hours')->select();
售前咨詢
售后咨詢
備案咨詢
二維碼
TOP