phpActiveRecord不正确的DateTimeFormat

sil*_*son 5 php mysql phpactiverecord

当尝试使用phpActiveRecord在表中创建记录时,我收到以下错误:

Invalid datetime format: 1292 Incorrect datetime value: '2013-06-20 11:59:08 PDT' for column 'created_at'

正在运行的代码:

$new_cart = new QuoteRequest();
$new_cart->status = "cart";
$new_cart->save();
Run Code Online (Sandbox Code Playgroud)

我已将其跟踪到phpActiveRecord中的相关行.文件Connection.php,第55-59行:

/**
 * Database's datetime format
 * @var string
 */
static $datetime_format = 'Y-m-d H:i:s T';
Run Code Online (Sandbox Code Playgroud)

使用它的行(Connection.php,第457-466行):

/**
 * Return a date time formatted into the database's datetime format.
 *
 * @param DateTime $datetime The DateTime object
 * @return string
 */
public function datetime_to_string($datetime)
{
  return $datetime->format(static::$datetime_format);
}
Run Code Online (Sandbox Code Playgroud)

转换值的位置(Table.php第394-412行):

private function &process_data($hash)
{
    if (!$hash)
        return $hash;

    foreach ($hash as $name => &$value)
    {
        if ($value instanceof \DateTime)
        {
            if (isset($this->columns[$name]) && $this->columns[$name]->type == Column::DATE)
                $hash[$name] = $this->conn->date_to_string($value);
            else
                $hash[$name] = $this->conn->datetime_to_string($value);
        }
        else
            $hash[$name] = $value;
    }
    return $hash;
}
Run Code Online (Sandbox Code Playgroud)

我使用的是MySQL 5.6.10版,该created_at字段是时间戳.

问题:这里phpActiveRecord有问题,还是MySQL问题?

sil*_*ire 7

static $datetime_format = 'Y-m-d H:i:s T';
Run Code Online (Sandbox Code Playgroud)

我认为你应该删除它'T'(它给你PDT,即时区缩写),因为它不是时间戳格式的一部分.

应该这样:

static $datetime_format = 'Y-m-d H:i:s';
Run Code Online (Sandbox Code Playgroud)

  • 刚遇到同样的事情.我的MySQL服务器(5.5.31)也在`T`上窒息.在不更改ActiveRecord代码的情况下,您可以覆盖以下格式:`ActiveRecord\Connection :: $ datetime_format ='Ymd H:i:s';` (4认同)