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

Lar*_*ner 4 php mysql mysqli timestamp date

我有这个数据库(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...
Run Code Online (Sandbox Code Playgroud)

但我无法将该timestamp数据提交到startTime女巫的数据库中type timestamp

为什么?怎么做?

Kev*_*vin 6

如果您的列数据类型是TIMESTAMP,那么它应该是:

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

注意:->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();
}
Run Code Online (Sandbox Code Playgroud)