国产欧美日韩第一页|日本一二三不卡视频|在线精品小视频,亚洲第一免费播放区,metcn人体亚洲一区,亚洲精品午夜视频

thinkphp5 時間比較查詢

2019-04-01 01:27:03 7148

時間比較

使用where方法

where方法支持時間比較,例如:


// 大于某個時間

where('create_time','>','2019-1-1');

// 小于某個時間

where('create_time','<=','2019-1-1');

// 時間區(qū)間查詢

where('create_time','between',['2018-1-1','2019-1-1']);

第三個參數(shù)可以傳入任何有效的時間表達式,會自動識別你的時間字段類型,支持的時間類型包括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();

// 時間區(qū)間查詢

Db::table('think_user')->whereTime('birthday', 'between', ['1970-10-1', '2000-10-1'])->select();

// 不在某個時間區(qū)間

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+版本開始,還可以使用下面的方式進行時間查詢


// 查詢兩個小時內(nèi)的博客Db::table('think_blog')->whereTime('create_time','-2 hours')->select();


提交成功!非常感謝您的反饋,我們會繼續(xù)努力做到更好!

這條文檔是否有幫助解決問題?

非常抱歉未能幫助到您。為了給您提供更好的服務,我們很需要您進一步的反饋信息:

在文檔使用中是否遇到以下問題: