CI 4 DateTime类的对象无法转换为字符串



我从Twilio的API获得一个对象。更具体地说,我使用以下函数https://www.twilio.com/docs/conversations/api/conversation-participant-resource

当我尝试插入de datetime对象到mysql时,我收到了这篇文章中解释的错误。

另外,我测试了更改字段类型设置为日期,日期时间,文本或varchar,但没有任何工作。

谁能给我点提示吗?非常感谢任何帮助由于模型:

$sql=[
"id_conversation"=>$conversation_id,
"identity"=>$record->identity,
"identity_proxy_address"=>$explode_whatsapp[1],
"date_created"=>$record->dateCreated,
"date_updated"=>$record->dateUpdated,
];
$builder->insert($sql);

另一种方法也试过了:

$begin = new DateTime($record->dateCreated);
error: DateTime::__construct() expects parameter 1 to be string, object given

这个错误说的是它不能使用DateTime的实例作为字符串,这与您的数据库列类型或您如何实例化DateTime实例无关。

由于$record->dateCreated$record->dateUpdated已经是DateTime的实例,您需要将日期作为字符串获取:

"date_created" => $record->dateCreated->format('Y-m-d H:i:s'),
"date_updated" => $record->dateUpdated->format('Y-m-d H:i:s'),

(并保持您的数据库列类型为datetime)

最新更新