使用JSON / PHP将DATETIME插入到mysql数据库中

时间:2017-04-16 16:48:57

标签: php mysql json datetime

我有一个包含DATETIME列的数据库,我想使用JSON / PHP将当前日期插入其中,但我一直有这个错误:

第13行的/storage/h3/744/754744/public_html/SendBookingReq.php中无法将类DateTime的对象转换为字符串  这是我的php文件

<?php
require("password.php");
$connect = mysqli_connect("localhost", XXXX, XXXX, XXXX);
$driver_id = $_POST["driver_id"];
$email = $_POST["email"];
$duree = $_POST["duree"];
$distance = $_POST["distance"];
$response = array();
$dt_obj = new DateTime($response['DateTime'], new 
DateTimeZone('America/Chicago')); 
$dt_obj->setTimezone(new DateTimeZone('Europe/Paris')); 
$dt_obj->format('d-m-Y H:i:s');
echo $dt_obj; 
 function AddRequest() {
    global $connect, $driver_id, $email, $duree, $distance, $dt_obj ;
    $statement = mysqli_prepare($connect, "INSERT INTO demande (driver_id, pass_id, duree, distance, send_moment) VALUES (?, (SELECT user_id FROM passager WHERE email = ?), ?, ?, '$dt_obj')");
    mysqli_stmt_bind_param($statement, "isdis", $driver_id, $email, $duree, $distance,$dt_obj);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);     
}

    $response["success"] = false;  

    AddRequest();
    $response["success"] = true;  

echo json_encode($response);
 ?>

老实说,我看了很多关于我应该用datetime ex写的字母:“s”表示字符串,但我没找到。

2 个答案:

答案 0 :(得分:2)

不要从应用程序中插入datetime值。由于时钟偏差,时区,延迟等原因,应用程序级时间戳很麻烦。使用数据库服务器日期时间可以避免这些问题。

根据数据库日期时间设置它。最简单的方法是默认send_moment以获得插入新行时的当前日期时间。这在CREATE TABLE语句中处理(参见此处)。

如果您打算在声明中这样做,我建议您使用now()

INSERT INTO demande (driver_id, pass_id, duree, distance, send_moment)  
   SELECT ?, user_id, ?, ?, NOW()
   FROM passager
   WHERE email = ?;

请注意,这会稍微改变参数的顺序。电子邮件现在是最后一个参数而不是第二个参数。

我认为绑定语句是:

mysqli_stmt_bind_param($statement, "isis", $driver_id, $duree, $distance, $email);

您必须仔细检查类型。

答案 1 :(得分:1)

  

错误

     

类DateTime的对象无法转换为字符串   第13行/storage/h3/744/754744/public_html/SendBookingReq.php

由于第13行的echo $dt_obj而发生此错误,但echo输出一个或多个字符串。您可能希望改为使用var_dump($dt_obj)print_r($dt_obj)

以后编辑

<?php
require("password.php");
$connect = mysqli_connect("localhost", XXXX, XXXX, XXXX);
$driver_id = $_POST["driver_id"];
$email = $_POST["email"];
$duree = $_POST["duree"];
$distance = $_POST["distance"];
$response = array();
$dt_obj = new DateTime($response['DateTime'], new 
DateTimeZone('America/Chicago')); 
$dt_obj->setTimezone(new DateTimeZone('Europe/Paris'));

/* create a DATETIME string to use in mysqli_stmt_bind_param() in line 24
*  https://dev.mysql.com/doc/refman/5.5/en/date-and-time-literals.html */ 
$send_time = $dt_obj->format('Y-d-m H:i:s');

function AddRequest() {
    global $connect, $driver_id, $email, $duree, $distance, $send_time ;
    echo($send_time);
    /* proper number of parametres (?) according to the db table's number of columns (5) */
    $statement = mysqli_prepare($connect, "INSERT INTO demande (driver_id, pass_id, duree, distance, send_moment) 
                                           VALUES (?, (SELECT user_id FROM passager WHERE email = ?), ?, ?, ?)");

    /* http://php.net/manual/en/mysqli-stmt.bind-param.php */
    mysqli_stmt_bind_param($statement, "isdis", $driver_id, $email, $duree, $distance,$send_time);
    mysqli_stmt_execute($statement);
    mysqli_stmt_close($statement);     
}

$response["success"] = false;  

AddRequest();
$response["success"] = true;  

echo json_encode($response);
?>