thinkphp5出现A non well formed numeric value encountered的解决办法修改formatDateTime方法如下
默认值:
if (is_null($this->autoWriteTimestamp)) {
// 自动写入时间戳默认false 包含datetime、date、timestamp
$this->autoWriteTimestamp = $this->getQuery()->getConfig('auto_timestamp');//getConfig:获取database.php的配置参数或者convention.php下的database项
}
if (is_null($this->dateFormat)) {
// 设置时间戳格式默认Y-m-d H:i:s
$this->dateFormat = $this->getQuery()->getConfig('datetime_format');
}
if (is_null($this->resultSetType)) {
//数据集返回类型默认array 可选collection(返回collection对象)
$this->resultSetType = $this->getQuery()->getConfig('resultset_type');
}
tp获取器部分逻辑:
存在$this->createTime或者$this->updateTime的情况下
如果$this->autoWriteTimestamp的值是datetime、date、timestamp其中一种则
执行 $value = $this->formatDateTime(strtotime($value), $this->dateFormat);
否则(值为其他情况如true):
$value = $this->formatDateTime($value, $this->dateFormat);
所以要不就关闭自动更新时间 否则formatDateTime方法第一个参数可能会出现两种情况中的一种(时间戳与非时间戳)
/**
* 时间日期字段格式化处理
* @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 && false !== $format) {# !timestamp默认false走这里 设置为date timestap datetime则不走这里
#修改开始位置
if (!preg_match("/^[0-9]{10}|[0-9]{13}$/",$time)) {
$time=strtotime($time);
}#修改结束位置
$time = date($format, $time);
}
return $time;#直接返回
}
或者在模型文件:修改
protected $autoWriteTimestamp=false; // 是否需要自动写入时
又或者修改datebase.php
'datetime_format' = false