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

jqjz2hbq  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(194)

我正在从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
ifmq2ha2

ifmq2ha21#

该错误说明它不能将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'),

(And将数据库列类型保留为datetime

相关问题