From d07c5eff8317f022107d080994d3a29d33c7aef4 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 23 Dec 2016 10:56:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E6=88=B3=E5=AD=97=E6=AE=B5=E5=86=99=E5=85=A5=E5=92=8C?= =?UTF-8?q?=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 43 ++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 4290cf68..13ed8fc1 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -328,31 +328,44 @@ abstract class Model implements \JsonSerializable, \ArrayAccess case 'datetime': case 'date': $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, $_SERVER['REQUEST_TIME']); + $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $format); break; case 'timestamp': case 'integer': default: $value = $_SERVER['REQUEST_TIME']; - if (strpos($type, '\\')) { + if (false !== strpos($type, '\\')) { // 传入类名 $value = new $type($value); } break; } } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { - $value = date($this->dateFormat, $_SERVER['REQUEST_TIME']); + $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $this->dateFormat); } else { - $value = $_SERVER['REQUEST_TIME']; - if (is_string($this->autoWriteTimestamp) && strpos($this->autoWriteTimestamp, '\\')) { - // 传入类名 - $class = $this->autoWriteTimestamp; - $value = new $class($value); - } + $value = $this->formatDateTime($value, $this->dateFormat, true); } return $value; } + /** + * 时间日期字段格式化处理 + * @access public + * @param mixed $time 时间日期表达式 + * @param mixed $format 日期格式 + * @param bool $timestamp 是否进行时间戳转换 + * @return mixed + */ + protected function formatDateTime($time, $format, $timestamp = false) + { + if (false !== strpos($format, '\\')) { + $time = new $format($time); + } elseif (!$timestamp) { + $time = date($format, $time); + } + return $time; + } + /** * 数据写入 类型转换 * @access public @@ -388,7 +401,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess break; case 'datetime': $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, is_numeric($value) ? $value : strtotime($value)); + if (false === strpos($format, '\\')) { + $value = date($format, is_numeric($value) ? $value : strtotime($value)); + } break; case 'object': if (is_object($value)) { @@ -404,6 +419,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess case 'serialize': $value = serialize($value); break; + } return $value; } @@ -477,13 +493,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess case 'timestamp': if (!is_null($value)) { $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, $value); + $value = $this->formatDateTime($value, $format); } break; case 'datetime': if (!is_null($value)) { $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, strtotime($value)); + $value = $this->formatDateTime(strtotime($value), $format); } break; case 'json': @@ -499,7 +515,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $value = unserialize($value); break; default: - if (strpos($type, '\\')) { + if (false !== strpos($type, '\\')) { + // 对象类型 $value = new $type($value); } }