简体   繁体   English

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

[英]Submit timestamp to mysql database with php mysqli

I have this database (mysql): 我有这个数据库(mysql):

在此处输入图片说明

And I have code: 我有代码:

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

but I cant submit that timestamp data into database in startTime witch have type timestamp 但我无法将timestamp数据提交到startTime数据库中, type timestamp

Why? 为什么? How to do that? 怎么做?

If your column data type is TIMESTAMP , then it should be: 如果您的列数据类型为TIMESTAMP ,则应为:

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

Note: ->getTimestamp() returns unix timestamp 注意: ->getTimestamp()返回unix时间戳

And modify your wrapper function: 并修改包装函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM