I got problem by getting date value from mysql through php. After I added the date, then no thing returned. What's the error of the code?
The column datatype is DateTime in mysql.
After output as Json, I use the Json formatter online one, and it said it is not valid Json. why?
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;
?>
'Release Date' => (date) $r['Release date'],
You're Typecasting to a PHP datatype of date
..... there is no such datatype in PHP
If you're trying to format the date, then use PHP's standard date formatting functions and methods
EDIT
example
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']);
}