我想将通过 HTTP POST 获得的时间字符串插入到带有时间戳字段的 mysql 表中。我该怎么做?
我的PHP代码是
$time_string="Apr 23, 2019 4:30:15 PM";
$query = $database->prepare('INSERT INTO trips(trip_start) VALUES(?)') {
$query->bind_param("s", $time_string]);
$query->execute();
这里trip_start是MYSQL中的时间戳数据类型
最佳答案
我建议使用 Carbon使用日期格式的库,因为它使这个和其他操作变得非常容易。在这种特殊情况下,我会做这样的事情:
$carbonDate = new Carbon\Carbon('Apr 23, 2019 4:30:15 PM');
$timeStamp = $carbonDate->toDateTimeString();
现在 timeStamp
是 mysql 的有效时间戳。
关于php - 如何在 PHP 中将时间字符串转换为 sql 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55813933/