使用php mysqli将时间戳提交到mysql数据库

时间:2014-12-06 15:00:33

标签: php mysql date mysqli timestamp

我有这个数据库(mysql):

enter image description here

我有代码:

$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...

但是我无法将timestamp数据提交到startTime的{​​{1}}数据库中type timestamp

为什么呢?怎么做?

1 个答案:

答案 0 :(得分:4)

如果您的列数据类型为TIMESTAMP,则应为:

$datum = new DateTime();
$startTime = $datum->format('Y-m-d H:i:s');

注意:->getTimestamp()会返回 unix时间戳

修改你的包装函数:

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();
}