tp5 報 A non well formed numeric value encountered 的錯解決辦法


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

 

datetime、date、timestamp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM