溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用thinkPHP5框架怎么實(shí)現(xiàn)時(shí)間查詢操作

發(fā)布時(shí)間:2021-06-07 16:44:09 來源:億速云 閱讀:128 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章為大家展示了使用thinkPHP5框架怎么實(shí)現(xiàn)時(shí)間查詢操作,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

時(shí)間比較

使用where方法

where方法支持時(shí)間比較,例如:

// 大于某個(gè)時(shí)間
where('create_time','> time','2016-1-1');
// 小于某個(gè)時(shí)間
where('create_time','<= time','2016-1-1');
// 時(shí)間區(qū)間查詢
where('create_time','between time',['2015-1-1','2016-1-1']);

使用whereTime方法

whereTime方法提供了日期和時(shí)間字段的快捷查詢,示例如下:

// 大于某個(gè)時(shí)間
Db::table('think_user')->whereTime('birthday', '>=', '1970-10-1')->select();
// 小于某個(gè)時(shí)間
Db::table('think_user')->whereTime('birthday', '<', '2000-10-1')->select();
// 時(shí)間區(qū)間查詢
Db::table('think_user')->whereTime('birthday', 'between', ['1970-10-1', '2000-10-1'])->select();
// 不在某個(gè)時(shí)間區(qū)間
Db::table('think_user')->whereTime('birthday', 'not between', ['1970-10-1', '2000-10-1'])->select();

時(shí)間表達(dá)式

還提供了更方便的時(shí)間表達(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)天、本周、本月和今年的時(shí)間,還可以簡化為:

// 獲取今天的博客
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)行時(shí)間查詢
// 查詢兩個(gè)小時(shí)內(nèi)的博客
Db::table('think_blog')->whereTime('create_time','-2 hours')->select();

上述內(nèi)容就是使用thinkPHP5框架怎么實(shí)現(xiàn)時(shí)間查詢操作,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI