手册目录
前言[随时更新]
MySQL快速复习
MySQL原生查询
选择数据表
删除数据
聚合查询
时间查询
视图查询
子查询
总结/参考
whereTime可完全用where方法代替
/**
* 查询日期或者时间
* @access public
* @param string $field 日期字段名
* @param string $op 比较运算符或者表达式
* @param string|array $range 比较范围
* @return $this
*/
public function whereTime($field, $op, $range = null)
{
if (is_null($range)) {
// 使用日期表达式
$date = getdate();
switch (strtolower($op)) {
case 'today':
case 'd':
$range = 'today';
break;
case 'week':
case 'w':
$range = 'this week 00:00:00';
break;
case 'month':
case 'm':
$range = mktime(0, 0, 0, $date['mon'], 1, $date['year']);
break;
case 'year':
case 'y':
$range = mktime(0, 0, 0, 1, 1, $date['year']);
break;
case 'yesterday':
$range = ['yesterday', 'today'];
break;
case 'last week':
$range = ['last week 00:00:00', 'this week 00:00:00'];
break;
case 'last month':
$range = [date('y-m-01', strtotime('-1 month')), mktime(0, 0, 0, $date['mon'], 1, $date['year'])];
break;
case 'last year':
$range = [mktime(0, 0, 0, 1, 1, $date['year'] - 1), mktime(0, 0, 0, 1, 1, $date['year'])];
break;
}
$op = is_array($range) ? 'between' : '>';
}
$this->where($field, strtolower($op) . ' time', $range);
return $this;
}
源码分析:
array(11) {
["seconds"] => int(46)
["minutes"] => int(59)
["hours"] => int(16)
["mday"] => int(13)
["wday"] => int(0)
["mon"] => int(11)
["year"] => int(2016)
["yday"] => int(317)
["weekday"] => string(6) "Sunday"
["month"] => string(8) "November"
[0] => int(1479027586)
}
此时,将该方法的第三个参数:range视为时间表达式,根据它的值进行判断,从$date数组中取中对应的数据:
mktime(小时,分钟,秒, 月,日,年),返回时间戳,strtotime()将日期型字符串转时间戳;
如果$range是数组,则把操作符$op变更成:between,进行区间查询
最后仍是调用 where 方法完成查询
与上节介绍的where非常相似,可视为where中专门针对日期字段查询的特例。语法上看,可以简单记为:查询表达式中的time,做为方法名单词第二部分即可。即将where中查询表达式中的time,移到方法名where后面做后缀,即whereTime 。
Db::table('表名') -> field('字段列表') ->whereTime('日期类型字段','条件表达式', '日期格式') ->select();
我们仍以上节课的例子,演示比较查询和区间查询
<?php
namespace app\index\controller;
//导入数据库类
use think\Db;
class Index {
public function index(){
$result = Db::table('tp5_staff')
-> field('id,name,hiredate')
->whereTime('hiredate','>', '2015-01-01')
->select();
dump($result);
}
}
array(5) {
[0] => array(3) {
["id"] => int(1006)
["name"] => string(9) "西门庆"
["hiredate"] => string(10) "2015-12-25"
}
[1] => array(3) {
["id"] => int(1007)
["name"] => string(9) "潘金莲"
["hiredate"] => string(10) "2016-03-14"
}
[2] => array(3) {
["id"] => int(1008)
["name"] => string(6) "宋江"
["hiredate"] => string(10) "2015-12-31"
}
[3] => array(3) {
["id"] => int(1023)
["name"] => string(9) "段王爷"
["hiredate"] => string(10) "2015-12-31"
}
[4] => array(3) {
["id"] => int(1028)
["name"] => string(6) "方方"
["hiredate"] => string(10) "2015-12-31"
}
}
SELECT `id`,`name`,`hiredate` FROM `tp5_staff` WHERE `hiredate` > '2015-01-01'
<?php
namespace app\index\controller;
//导入数据库类
use think\Db;
class Index {
public function index(){
$result = Db::table('tp5_staff')
-> field('id,name,hiredate')
->whereTime('hiredate','between', ['2015-01-01','2016-01-01'])
->select();
dump($result);
}
}
array(4) {
[0] => array(3) {
["id"] => int(1006)
["name"] => string(9) "西门庆"
["hiredate"] => string(10) "2015-12-25"
}
[1] => array(3) {
["id"] => int(1008)
["name"] => string(6) "宋江"
["hiredate"] => string(10) "2015-12-31"
}
[2] => array(3) {
["id"] => int(1023)
["name"] => string(9) "段王爷"
["hiredate"] => string(10) "2015-12-31"
}
[3] => array(3) {
["id"] => int(1028)
["name"] => string(6) "方方"
["hiredate"] => string(10) "2015-12-31"
}
}
SELECT `id`,`name`,`hiredate` FROM `tp5_staff` WHERE `hiredate` BETWEEN '2015-01-01' AND '2016-01-01'

whereTime 与 where 无本质区别,可视为where针对日期字段的快捷方式,实际开发,具体用哪个随个人喜好,既然用了TP5,就尽可能选择使用TP5的新特性。
科技资讯
24小时阅读榜
1
2
3
4
5
6
7
8
9
10
精品课程
共5课时 | 18万人学习
共49课时 | 82.1万人学习
共29课时 | 65万人学习
共25课时 | 41.3万人学习
共43课时 | 76.7万人学习