我有这个数据库(mysql):
我有代码:
$day = $app->request->post('day');
$startLocation = $app->request->post('startLocation');
$datum = new DateTime();
$startTime = $datum->getTimestamp(); //this line is IMPORTANT
global $user_id;
$db = new DbHandler();
// creating new task
$day_id = $db->createDay($user_id, $day, $startLocation, $startTime); ETC...
但我无法在 startTime
中将 timestamp
数据提交到数据库中,女巫有 type timestamp
为什么?如何做到这一点?
最佳答案
如果您的列数据类型是 TIMESTAMP
,那么它应该是:
$datum = new DateTime();
$startTime = $datum->format('Y-m-d H:i:s');
注意:->getTimestamp()
返回 unix timestamp
并修改你的包装函数:
public function createDay($user_id, $day, $startLocation, $startTime) {
$stmt = $this->conn->prepare("INSERT INTO days(day,startLocation,startTime) VALUES(?,?,?)");
$stmt->bind_param('sss', $day, $startLocation, $startTime); // change the d into s in your types
$result = $stmt->execute();
$stmt->close();
}
关于php - 使用 php mysqli 将时间戳提交到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27332954/