時間比較
1. 使用where方法
支持timestamps、datetime躏敢、date和int時間類型(根據(jù)數(shù)據(jù)庫字段設(shè)置的類型)
where('create_time','> time','2017/9/20 9:36:2','int')//==>WHERE `create_time` > 1505871362"
// 時間區(qū)間查詢
where('create_time','between time',['2015-1-1','2016-1-1']);
2. whereTime
whereTime
方法提供了日期和時間字段的快捷查詢
//===>"SELECT * FROM `cms_user` WHERE `create_time` > 1505871362"
Db::table('cms_user')->whereTime('create_time','>','2017/9/20 9:36:2','int')->select();
// 時間區(qū)間查詢
Db::table('cms_user')->whereTime('birthday', 'between', ['1970-10-1', '2000-10-1'])->select();
3. 時間表達(dá)式
// 獲取今天的博客
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();
如果查詢當(dāng)天畅卓、本周裕循、本月和今年的時間蜂绎,還可以簡化為:
// 獲取今天的博客
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+
版本開始搓逾,還可以使用下面的方式進(jìn)行時間查詢
// 查詢兩個小時內(nèi)的博客
Db::table('think_blog')->whereTime('create_time','-2 hours')->select();