php - 如何在 PHP 中将时间字符串转换为 sql 时间戳

标签 php mysql sql

我想将通过 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/

相关文章:

php - PHP中如何实现imagecreatefromstring的反向效果?

php - 如何在两个 while 循环中完成这个复杂的查询

PHP/Mysql -> 制作索引/分类列表的最佳方法

java - 错误 : Make sure the Cursor is initialized correctly before accessing data from it?

mysql - sql时间实例频率

php - 根据表中选中的动态复选框更新 MySQL 数据库中的数据

php - append 按钮 onclick 不要触发 Jquery

php - 使用 jquery ajax 提交显示进度条直至响应发生

php - 从 mysql 中的 3 列中选择文件并压缩它们

SQLite 使用新生成的 id 插入