Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PDO的php/mysql日期时间格式_Php_Json_Date - Fatal编程技术网

使用PDO的php/mysql日期时间格式

使用PDO的php/mysql日期时间格式,php,json,date,Php,Json,Date,我通过php从mysql获取日期值时遇到了问题。在我加上日期之后,没有任何东西返回。代码的错误是什么 在mysql中,列数据类型是DateTime 输出为Json后,我使用Json格式化程序online one,它说它不是有效的Json。为什么? try { $conn = new PDO("mysql:host=host;dbname=db", "username", "pwd!"); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO:

我通过php从mysql获取日期值时遇到了问题。在我加上日期之后,没有任何东西返回。代码的错误是什么

在mysql中,列数据类型是DateTime

输出为Json后,我使用Json格式化程序online one,它说它不是有效的Json。为什么?

try {
    $conn = new PDO("mysql:host=host;dbname=db", "username", "pwd!");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $Ticker = htmlspecialchars($_GET["Ticker"]);
    $Today = date('Ymd');

    $result = $conn->prepare("SELECT Ticker, date(dateOfRelease) as 'Release date', Amount FROM TradeQuote WHERE Ticker='$Ticker' GROUP BY Date(dateOfRelease)");

    $result = $result->fetchAll(); 
    $temp = array();

    foreach($result as $r) {

          $temp[] = array('Ticker' => (string) $r['Ticker'], 'Release Date' => (date) $r['Release date'], 'Price' => (string) $r['Price'], 'Amount' => (string) $r['Amount']); 

        }
    $table = $temp;
    $jsonTable = json_encode($table);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
    echo $jsonTable;

?>
您正在将类型转换为PHP数据类型
date
。。。。。PHP中没有这样的数据类型

如果要格式化日期,请使用PHP的标准日期格式化函数和方法

编辑

范例

foreach($result as $r) {
    $releaseDate = new DateTime((string) $r['Release date']);
    $temp[] = array('Ticker' => (string) $r['Ticker'], 'Release Date' => $releaseDate->format('Y-m-d'), 'Price' => (string) $r['Price'], 'Amount' => (string) $r['Amount']); 

}

请你给出具体的例子,或者我可以参考的链接也足够好,它可能对你来说很简单,但对我来说不是。谢谢
foreach($result as $r) {
    $releaseDate = new DateTime((string) $r['Release date']);
    $temp[] = array('Ticker' => (string) $r['Ticker'], 'Release Date' => $releaseDate->format('Y-m-d'), 'Price' => (string) $r['Price'], 'Amount' => (string) $r['Amount']); 

}