如何在mysql数据库中存储时间?

时间:2016-12-10 07:48:00

标签: php mysql angularjs

Html

<input type="date" ng-model="bookdate" required>

 <input type="time" ng-model="booktime">

控制器

$scope.insertData=function(){

 bookdate=$scope.bookdate;   
    booktime=$scope.booktime;          
 $http.post("url", {
'bookdate':bookdate,
        'booktime':booktime})

PHP

日期列数据类型是日期时间数据类型是时间

 $data = json_decode(file_get_contents("php://input"));
    $bookdate =$data->bookdate;
    $booktime=$data->booktime; 
$newtime = date("H:i:s", strtotime($data->booktime));

$conn = new mysqli("localhost", "", "", "");

$sql = "INSERT INTO table(bookdate,booktime)
VALUES ('$bookdate','$newtime')";
if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

在提交日期的数据库中,存储以前的日期和时间值为00:00:00

1 个答案:

答案 0 :(得分:2)

您可以尝试使用strtotime()。

尝试以下

$newdate = date("Y-m-d", strtotime($data->bookdate));
$newtime = date("H:i:s", strtotime($data->booktime));

只要您获得$ data-&gt; bookdate和$ data-&gt; booktime的值,就应该这样做。

继续在mysql数据库中输入新变量,如下所示。

"INSERT INTO yourdb(booktime) VALUES('$newtime')"