0

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;

?>

1 Answer 1

1
'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']); 

}
Sign up to request clarification or add additional context in comments.

1 Comment

could you please give specific example or the link that I could refer to is also good enough, it may be simple to you but not me. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.