尝试使用 phpActiveRecord 在表中创建记录时出现以下错误:
无效的日期时间格式:1292 不正确的日期时间值:“created_at”列的“2013-06-20 11:59:08 PDT”
正在运行的代码:
$new_cart = new QuoteRequest();
$new_cart->status = "cart";
$new_cart->save();
我已经追踪到 phpActiveRecord 中的相关行。文件 Connection.php,第 55-59 行:
/**
* Database's datetime format
* @var string
*/
static $datetime_format = 'Y-m-d H:i:s T';
以及使用它的行(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);
}
以及转换值的位置(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;
}
我使用的是 MySQL 5.6.10 版,created_at
字段是一个时间戳。
问题:是不是这里的phpActiveRecord有问题,还是MySQL的问题?
最佳答案
static $datetime_format = 'Y-m-d H:i:s T';
我认为您应该删除 'T'
(它给您 PDT,即时区缩写),因为它不是时间戳格式的一部分。
应该是这样的:
static $datetime_format = 'Y-m-d H:i:s';
关于phpActiveRecord 不正确的日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17222180/