tp5(thinkPHP5框架)時(shí)間查詢(xún)操作實(shí)例分析
本文實(shí)例講述了tp5(thinkPHP5框架)時(shí)間查詢(xún)操作。分享給大家供大家參考,具體如下:
在項(xiàng)目中 可能會(huì)遇到 跨月份進(jìn)行查詢(xún)
比如在 當(dāng)輸入201809 會(huì)獲取當(dāng)月的開(kāi)始時(shí)間$start_month 和 結(jié)束時(shí)間 $end_month
會(huì)查詢(xún)2018年9月份的數(shù)據(jù) 但是當(dāng)其中的一個(gè)數(shù)據(jù)是在201809到201810 ,數(shù)據(jù)庫(kù)的字段是 start_time end_time
這時(shí)候
Db::name("表名")->where('start_time','<= time',$end_month)
->where('end_time','> time',$start_month)
->select();
時(shí)間比較
使用where方法
where方法支持時(shí)間比較,例如:
// 大于某個(gè)時(shí)間
where('create_time','> time','2016-1-1');
// 小于某個(gè)時(shí)間
where('create_time','<= time','2016-1-1');
// 時(shí)間區(qū)間查詢(xún)
where('create_time','between time',['2015-1-1','2016-1-1']);
使用whereTime方法
whereTime方法提供了日期和時(shí)間字段的快捷查詢(xún),示例如下:
// 大于某個(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ū)間查詢(xún)
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á)式查詢(xún),例如:
// 獲取今天的博客
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();
如果查詢(xún)當(dāng)天、本周、本月和今年的時(shí)間,還可以簡(jiǎn)化為:
// 獲取今天的博客
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+版本開(kāi)始,還可以使用下面的方式進(jìn)行時(shí)間查詢(xún)
// 查詢(xún)兩個(gè)小時(shí)內(nèi)的博客
Db::table('think_blog')->whereTime('create_time','-2 hours')->select();
參考地址:https://www.kancloud.cn/he_he/thinkphp5
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門(mén)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- ThinkPHP5查詢(xún)數(shù)據(jù)及處理結(jié)果的方法小結(jié)
- thinkPHP5實(shí)現(xiàn)的查詢(xún)數(shù)據(jù)庫(kù)并返回json數(shù)據(jù)實(shí)例
- tp5(thinkPHP5)框架數(shù)據(jù)庫(kù)Db增刪改查常見(jiàn)操作總結(jié)
- tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)查詢(xún)的方法
- ThinkPHP5聯(lián)合(關(guān)聯(lián))查詢(xún)、多條件查詢(xún)與聚合查詢(xún)實(shí)例詳解
- thinkPHP5框架閉包函數(shù)與子查詢(xún)傳參用法示例
- ThinkPHP5框架緩存查詢(xún)操作分析
- thinkPHP5框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)連接,跨數(shù)據(jù)連接查詢(xún)操作示例
- Thinkphp5.0框架的Db操作實(shí)例分析【連接、增刪改查、鏈?zhǔn)讲僮鞯取?/a>
- ThinkPHP5.1框架數(shù)據(jù)庫(kù)鏈接和增刪改查操作示例
- tp5.1 框架join方法用法實(shí)例分析
相關(guān)文章
PHP的error_reporting錯(cuò)誤級(jí)別變量對(duì)照表
這篇文章主要介紹了PHP的error_reporting錯(cuò)誤級(jí)別變量對(duì)照表,需要的朋友可以參考下2014-07-07
PHP中exec函數(shù)和shell_exec函數(shù)的區(qū)別
這篇文章主要介紹了PHP中exec函數(shù)和shell_exec函數(shù)的區(qū)別,這兩個(gè)函數(shù)是非常危險(xiǎn)的函數(shù),一般情況都是被禁用的,當(dāng)然特殊情況下也會(huì)使用,需要的朋友可以參考下2014-08-08
ThinkPHP權(quán)限認(rèn)證Auth實(shí)例詳解
這篇文章主要介紹了ThinkPHP權(quán)限認(rèn)證Auth實(shí)例,需要的朋友可以參考下2014-07-07
laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法示例
這篇文章主要介紹了laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法,結(jié)合實(shí)例形式詳細(xì)分析了laravel5.6 框架使用Eloquent ORM操作數(shù)據(jù)增刪改查相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-01-01
php/js獲取客戶(hù)端mac地址的實(shí)現(xiàn)代碼
這篇文章主要介紹了如何在php與js中分別獲取客戶(hù)度mac地址的方法,需要的朋友可以參考下2013-07-07

