1. ホーム
  2. php

tp5 error with A non well formed numeric value encountered.

2022-02-08 06:56:16

Thinkphp5 で A non well formed numeric value encountered の解決策は、formatDateTime メソッドを次のように変更することです。

Default value.
if (is_null($this->autoWriteTimestamp)) {
// autoWriteTimestamp default false Contains datetime, date, timestamp
$this->autoWriteTimestamp = $this->getQuery()->getConfig('auto_timestamp');//getConfig: get the configuration parameters of database.php or convention.php under database item
}

if (is_null($this->dateFormat)) {
// Set the timestamp format to default Y-m-d H:i:s
$this->dateFormat = $this->getQuery()->getConfig('datetime_format');
}

if (is_null($this->resultSetType)) {

//dataset return type default array optional collection (return collection object)
$this->resultSetType = $this->getQuery()->getConfig('resultset_type');
}


The tp getter part of the logic.
The presence of $this->createTime or $this->updateTime
If the value of $this->autoWriteTimestamp is one of datetime, date, or timestamp then   
execute $value = $this->formatDateTime(strtotime($value), $this->dateFormat);
Otherwise (value is other case like true).
$value = $this->formatDateTime($value, $this->dateFormat);

So either turn off automatic time update or else the first parameter of the formatDateTime method may appear in one of two cases (timestamp vs. non-timestamp)
    /**
     * Formatting of time and date fields
     * @access public
     * @param mixed $time time date expression
     * @param mixed $format date format
     * @param bool $timestamp Whether to perform timestamp conversion
     * @return mixed
     */
    protected function formatDateTime($time, $format, $timestamp = false)
    {
        if (false ! == strpos($format, '\\')) {
            $time = new $format($time);
        } elseif (! $timestamp && false ! == $format) {

<未定義


# !timestamp default false goes here set to date timestap datetime does not go here
            # Modify the start position
            if (!preg_match("/^[0-9]{10}|[0-9]{13}$/",$time)) {
                $time=strtotime($time);
            }#Modify the end position
            $time = date($format, $time);
        }
        return $time;# return directly
    }

 もしくは、モデルファイルにおいて

    protected $autoWriteTimestamp=false; // Whether or not to automatically write the timestamp

またはdatebase.phpを修正する

'datetime_format' = false

datetime, date, timestamp

取得元:https://www.cnblogs.com/lichihua/p/11205143.html