Home > PHP Framework > ThinkPHP > ThinkPHP5.2: Time query (improvement, optimization)

ThinkPHP5.2: Time query (improvement, optimization)

爱喝马黛茶的安东尼
Release: 2019-12-16 13:54:39
forward
3505 people have browsed it

ThinkPHP5.2: Time query (improvement, optimization)

Version 5.2 improves the query of time fields. On the basis of simplifying the original time query method, three practical time query methods are added: whereYear/whereMonth/whereDay.

whereYear Query a certain year

// 查询今年数据
Blog::whereYear('create_time')->select();
// 查询去年数据
Blog::whereYear('create_time', 'last year')->select();
// 查询2019年数据
Blog::whereYear('create_time', '2019')->select();
Copy after login

whereMonth Query a certain month

// 查询本月数据
Blog::whereMonth('create_time')->select();
// 查询上月数据
Blog::whereMonth('create_time', 'last month')->select();
// 查询2019年1月数据
Blog::whereMonth('create_time', '2019-1')->select();
Copy after login

whereDay Query a certain day

// 查询今天数据
Blog::whereDay('create_time')->select();
// 查询昨天数据
Blog::whereDay('create_time', 'yesterday')->select();
// 查询2019-1-1数据
Blog::whereDay('create_time', '2019-1-1')->select();
Copy after login

In addition to the above usage of querying year, month and day, you can use whereBetweenTime to query data in any time interval.

// 查询2019-1-1~1-15日数据
Blog::whereBetweenTime('create_time', '2019-1-1', '2019-1-15')->select();
Copy after login

Only query data less than or greater than a certain time, use

// 查询2019-1-1以来的数据
Blog::whereTime('create_time', '>=', '2019-1-1')->select();
Copy after login

Therefore, with the whereTime/whereBetweenTime/whereBetweenTimeField method, it basically covers all time queries, and all The time query method supports any time field type, and it will be automatically processed without the need to do data conversion yourself when querying.

Many ThinkPHP introductory tutorials, all on the PHP Chinese website, welcome to learn online!

This article is reproduced from: https://blog.thinkphp.cn/962856

The above is the detailed content of ThinkPHP5.2: Time query (improvement, optimization). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:thinkphp.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template