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();
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();
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();
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();
Only query data less than or greater than a certain time, use
// 查询2019-1-1以来的数据 Blog::whereTime('create_time', '>=', '2019-1-1')->select();
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!