改进Model的时间戳字段写入和读取

This commit is contained in:
thinkphp
2016-12-23 10:56:37 +08:00
parent c47621294a
commit d07c5eff83

View File

@@ -328,31 +328,44 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case 'datetime': case 'datetime':
case 'date': case 'date':
$format = !empty($param) ? $param : $this->dateFormat; $format = !empty($param) ? $param : $this->dateFormat;
$value = date($format, $_SERVER['REQUEST_TIME']); $value = $this->formatDateTime($_SERVER['REQUEST_TIME'], $format);
break; break;
case 'timestamp': case 'timestamp':
case 'integer': case 'integer':
default: default:
$value = $_SERVER['REQUEST_TIME']; $value = $_SERVER['REQUEST_TIME'];
if (strpos($type, '\\')) { if (false !== strpos($type, '\\')) {
// 传入类名 // 传入类名
$value = new $type($value); $value = new $type($value);
} }
break; break;
} }
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { } 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 { } else {
$value = $_SERVER['REQUEST_TIME']; $value = $this->formatDateTime($value, $this->dateFormat, true);
if (is_string($this->autoWriteTimestamp) && strpos($this->autoWriteTimestamp, '\\')) {
// 传入类名
$class = $this->autoWriteTimestamp;
$value = new $class($value);
}
} }
return $value; 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 * @access public
@@ -388,7 +401,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
break; break;
case 'datetime': case 'datetime':
$format = !empty($param) ? $param : $this->dateFormat; $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; break;
case 'object': case 'object':
if (is_object($value)) { if (is_object($value)) {
@@ -404,6 +419,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case 'serialize': case 'serialize':
$value = serialize($value); $value = serialize($value);
break; break;
} }
return $value; return $value;
} }
@@ -477,13 +493,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case 'timestamp': case 'timestamp':
if (!is_null($value)) { if (!is_null($value)) {
$format = !empty($param) ? $param : $this->dateFormat; $format = !empty($param) ? $param : $this->dateFormat;
$value = date($format, $value); $value = $this->formatDateTime($value, $format);
} }
break; break;
case 'datetime': case 'datetime':
if (!is_null($value)) { if (!is_null($value)) {
$format = !empty($param) ? $param : $this->dateFormat; $format = !empty($param) ? $param : $this->dateFormat;
$value = date($format, strtotime($value)); $value = $this->formatDateTime(strtotime($value), $format);
} }
break; break;
case 'json': case 'json':
@@ -499,7 +515,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = unserialize($value); $value = unserialize($value);
break; break;
default: default:
if (strpos($type, '\\')) { if (false !== strpos($type, '\\')) {
// 对象类型
$value = new $type($value); $value = new $type($value);
} }
} }