From a9cfe2a801d8bb98c116e61fe48054423d8e32ce Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 13 Jun 2016 16:27:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E6=9C=9F=E5=92=8C?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=9F=A5=E8=AF=A2=E6=96=B9=E6=B3=95=20dateti?= =?UTF-8?q?me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Builder.php | 43 +++++++++++++++++++++++++++++++----- library/think/db/Query.php | 14 ++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 53c23e9b..c3bdf253 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -28,7 +28,7 @@ abstract class Builder protected $options = []; // 数据库表达式 - protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'exp' => 'EXP', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN', 'exists' => 'EXISTS', 'notexists' => 'NOT EXISTS', 'not exists' => 'NOT EXISTS', 'null' => 'NULL', 'notnull' => 'NOT NULL', 'not null' => 'NOT NULL']; + protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'exp' => 'EXP', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN', 'exists' => 'EXISTS', 'notexists' => 'NOT EXISTS', 'not exists' => 'NOT EXISTS', 'null' => 'NULL', 'notnull' => 'NOT NULL', 'not null' => 'NOT NULL', '> time' => '> TIME', '< time' => '< TIME', 'between time' => 'BETWEEN TIME', 'not between time' => 'NOT BETWEEN TIME']; // SQL表达式 protected $selectSql = 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%FORCE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%LOCK%%COMMENT%'; @@ -272,12 +272,10 @@ abstract class Builder } // where子单元分析 - protected function parseWhereItem($key, $val, $rule = '') + protected function parseWhereItem($field, $val, $rule = '') { - if ($key) { - // 字段分析 - $key = $this->parseKey($key); - } + // 字段分析 + $key = $field ? $this->parseKey($field) : ''; // 查询规则和条件 if (!is_array($val)) { @@ -340,6 +338,13 @@ abstract class Builder } else { $whereStr .= $exp . ' (' . $value . ')'; } + } elseif (in_array($exp, ['< TIME', '> TIME'])){ + $whereStr .= $key . ' ' . substr($exp,0,1) . ' ' . $this->parseDateTime($value, $field); + } elseif (in_array($exp, ['BETWEEN TIME', 'NOT BETWEEN TIME'])){ + if(is_string($value)){ + $value = explode(',',$value); + } + $whereStr .= $key . ' ' . substr($exp,0,-4) . $this->parseDateTime($value[0], $field) . ' AND ' . $this->parseDateTime($value[1], $field); } return $whereStr; } @@ -352,6 +357,32 @@ abstract class Builder return $query->buildSql($show); } + /** + * 日期时间条件解析 + * @access protected + * @param string $value + * @param string $key + * @return string + */ + protected function parseDateTime($value, $key) + { + // 获取时间字段类型 + $type = $this->query->getTableInfo('', 'type'); + if(isset($type[$key])){ + if(preg_match('/(datetime|timestamp)/is', $type[$key])){ + // 日期及时间戳类型 + $value = date('Y-m-d H:i:s', strtotime($value)); + }elseif(preg_match('/(date)/is', $type[$key])){ + // 日期及时间戳类型 + $value = date('Y-m-d', strtotime($value)); + }else{ + // 整型 + $value = strtotime($value); + } + } + return is_int($value)? $value : $this->connection->quote($value); + } + /** * limit分析 * @access protected diff --git a/library/think/db/Query.php b/library/think/db/Query.php index c82e5db8..977cdc4c 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1227,6 +1227,20 @@ class Query return $this; } + /** + * 查询日期或者时间 + * @access public + * @param string $field 日期字段名 + * @param string $op 比较运算 > < between not between + * @param string|array $range 比较范围 + * @return $this + */ + public function datetime($field, $op, $range = []) + { + $this->where($field, strtolower($op) . ' time', $range); + return $this; + } + /** * 获取数据表信息 * @access public