Store datetime without seconds using SQL in PHP -
i created function below in php insert ticker details in database. i'd store datetime field in database without seconds because json data updated every minute.
function insert(){ $jsonarray = json_decode(self::getticker(), true); var_dump($jsonarray); foreach ($jsonarray $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 `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 inserted return print_r(ticker_created_successfully); } else{ return print_r(ticker_create_failed); } }
the datetime column uses structure y-m-d h:i:s
it store seconds. can use php's datetime class format dates so:
$date = new datetime(); echo $date->format('y-m-d h:i'); // outputs 2017-08-18 17:05
when querying db, in clause can say:
...where datecolumn = '".$date->format('y-m-d h:i:00')."' etc
example:
$date = new datetime(); echo $date->format('y-m-d h:i:s')."\n"; // 2017-08-18 17:09:15 echo $date->format('y-m-d h:i:00')."\n"; // 2017-08-18 17:09:00
see in action: https://3v4l.org/d47pa
btw can pass in string datetime constructor
new datetime($row['datecolumn']);
Comments
Post a Comment