我在PHP中創建了下面這個函數來在數據庫中插入一些代碼詳細信息。我想在數據庫中存儲日期時間字段,因爲該json數據每分鐘更新一次。在PHP中使用SQL存儲日期時間(無秒)
function insert(){
$jsonArray = json_decode(self::getTicker(), true);
var_dump($jsonArray);
foreach ($jsonArray as $value) {
$high = $value['high'];
$low = $value['low'];
$last = $value['last'];
$buy = $value["buy"];
$sell = $value['sell'];
$date = $value['date'];
$vol = $value['vol'];
}
$dateF = gmdate("Y-m-d H:i:s", $date);
$sql = "INSERT INTO `ticker_history`
(`ticker_history_high`,
`ticker_history_low`,
`ticker_history_vol`,
`ticker_history_last`,
`ticker_history_buy`,
`ticker_history_sell`,
`ticker_history_date`)
VALUES
(?, ?, ?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($sql);
$stmt->bind_param("dddddss", $high,$low,$vol,$last,$buy,$sell,$dateF);
$result = $stmt->execute();
$stmt->close();
if ($result) {
// ticker successfully inserted
return print_r(TICKER_CREATED_SUCCESSFULLY);
} else{
return print_r(TICKER_CREATE_FAILED);
}
}
將'gmdate(「Y-m-d H:i:s」,$ date);''改成'gmdate(「Y-m-d H:i」,$ date);'? –
數據庫*中的'DATETIME'字段有*秒* - 它是數據類型的一部分...當然,如果將日期格式化爲'Ymd H:i:00',則將其格式化爲if你不需要秒。 – CD001
我試圖做到這一點,但它存儲的數據:00秒 –