Case Analysis of Time Query Operation in tp5 of thinkPHP5 Frame

  • 2021-12-11 17:28:00
  • OfStack

This article illustrates the tp5 (thinkPHP5 framework) time query operation. Share it for your reference, as follows:

You may encounter cross-month queries in your project

For example, when entering 201809, you will get the start time $start_month and the end time $end_month of the current month

The September 2018 data will be queried, but when one of the data is between 201809 and 201810, the database field is start_time end_time

At this time


Db::name(" Table name ")->where('start_time','<= time',$end_month)
->where('end_time','> time',$start_month)
->select();

Time comparison

Use the where method

The where method supports time comparisons, such as:


//  More than a certain time 
where('create_time','> time','2016-1-1');
//  Less than a certain time 
where('create_time','<= time','2016-1-1');
//  Time interval query 
where('create_time','between time',['2015-1-1','2016-1-1']);

Use the whereTime method

The whereTime method provides a quick query for the date and time fields, as shown below:


//  More than a certain time 
Db::table('think_user')->whereTime('birthday', '>=', '1970-10-1')->select();
//  Less than a certain time 
Db::table('think_user')->whereTime('birthday', '<', '2000-10-1')->select();
//  Time interval query 
Db::table('think_user')->whereTime('birthday', 'between', ['1970-10-1', '2000-10-1'])->select();
//  Not in a certain time interval 
Db::table('think_user')->whereTime('birthday', 'not between', ['1970-10-1', '2000-10-1'])->select();

Time expression

It also provides more convenient time expression queries, such as:


//  Get today's blog 
Db::table('think_blog') ->whereTime('create_time', 'today')->select();
//  Get yesterday's blog 
Db::table('think_blog')->whereTime('create_time', 'yesterday')->select();
//  Get this week's blog 
Db::table('think_blog')->whereTime('create_time', 'week')->select();
//  Get last week's blog 
Db::table('think_blog')->whereTime('create_time', 'last week')->select();
//  Get this month's blog 
Db::table('think_blog')->whereTime('create_time', 'month')->select();
//  Get last month's blog 
Db::table('think_blog')->whereTime('create_time', 'last month')->select();
//  Get this year's blog 
Db::table('think_blog')->whereTime('create_time', 'year')->select();
//  Get last year's blog 
Db::table('think_blog')->whereTime('create_time', 'last year')->select();

If you query the time of the day, this week, this month and this year, you can also simplify it to:


//  Get today's blog 
Db::table('think_blog')->whereTime('create_time', 'd')->select();
//  Get this week's blog 
Db::table('think_blog')->whereTime('create_time', 'w')->select();
//  Get this month's blog 
Db::table('think_blog')->whereTime('create_time', 'm')->select();
//  Get this year's blog 
Db::table('think_blog')->whereTime('create_time', 'y') ->select();
V5.0.5+ Version, you can also use the following methods for time query 
//  Look up blogs within two hours 
Db::table('think_blog')->whereTime('create_time','-2 hours')->select();

Reference address: https://www.kancloud.cn/he_he/thinkphp5

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: