问题描述
当尝试使用phpActiveRecord在表中创建记录时,出现以下错误:
When trying to create a record in a table using phpActiveRecord I get the following error:
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();
我已经在phpActiveRecord中找到了相关的内容.文件Connection.php,第55-59行:
I've tracked this down to the pertinent lines in phpActiveRecord. The file Connection.php, lines 55-59:
/**
* Database's datetime format
* @var string
*/
static $datetime_format = 'Y-m-d H:i:s T';
以及使用此代码的行(Connection.php,第457-466行):
And the line that uses this (Connection.php, lines 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);
}
值的转换位置(Table.php行394-412):
And where the value is converted (Table.php lines 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;
}
我正在使用MySQL版本5.6.10,并且created_at
字段是一个时间戳.
I am using MySQL version 5.6.10 and the created_at
field is a timestamp.
问题:此处的phpActiveRecord是否有问题,还是MySQL问题?
Question: Is there something wrong with phpActiveRecord here, or is it a MySQL problem?
推荐答案
static $datetime_format = 'Y-m-d H:i:s T';
我认为您应该删除'T'
(它为您提供 PDT ,即时区缩写),因为它不属于时间戳格式.
I think you should remove that 'T'
(which gives you PDT, i.e. the timezone abbreviation) as it is not part of the timestamp format.
应该这样:
static $datetime_format = 'Y-m-d H:i:s';
这篇关于phpActiveRecord不正确的DateTimeFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!