2016-12-18 74 views
1

我有一個數據庫與一些溫度數據和日期。爲了繪製數據,我需要將日期時間轉換爲ISO8601。 我發現命令來選擇正確的數據並將其轉換:檢索數據從一個特定的SQL命令在PHP

SELECT DATE_FORMAT(`datelog`, '%Y-%m-%dT%TZ') AS date_formatted FROM `tempo` ORDER BY id ASC 

在phpMyAdmin數據顯示正確,但是當我運行我的PHP腳本使用相同的命令它給我的數據,它們存儲在數據庫(例如:{「datelog」:「2016-12-18 11:54:11」,「donnee」:「16.937」})。我該如何改變它?

<?php 
//setting header to json 
header('Content-Type: application/json'); 

//database 
define('DB_HOST', '127.0.0.1'); 
define('DB_USERNAME', 'root'); 
define('DB_PASSWORD', '******'); 
define('DB_NAME', 'tempo'); 

//get connection 
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME); 

if(!$mysqli){ 
    die("Connection failed: " . $mysqli->error); 
} 

//query to get data from the table 
$query = "SELECT DATE_FORMAT(`datelog`, \'%Y-%m-%dT%TZ\') AS date_formatted\n" . "FROM `tempo`\n" . "ORDER BY id ASC "; 

//execute query 
$result = $mysqli->query($query); 

//loop through the returned data 
$data = array(); 
foreach ($result as $row) { 
    $data[] = $row; 
} 

//free memory associated with result 
$result->close(); 

//close connection 
$mysqli->close(); 

//now print the data 
print json_encode($data); 

回答

0

它可以在東西串聯:

$query = "SELECT DATE_FORMAT(`datelog`, \'%Y-%m-%dT%TZ\') AS date_formatted\n" . "FROM `tempo`\n" . "ORDER BY id ASC "; 

試試這個,如果功能:

$query = "SELECT DATE_FORMAT(`datelog`, \'%Y-%m-%dT%TZ\') AS date_formatted\n . " " . FROM `tempo`\n . " " . ORDER BY id ASC . "; 
+0

還是一樣輸出這一行 – eliobou

+0

例如,應該是你的輸出? – rcf2255

+0

它應該是這樣的:2016-12-18T00:58:05Z – eliobou