Return JSON with date and time

Asked

Viewed 556 times

1

I’m having a problem returning json with date and time the code is as follows:

<?php
$sql = "SELECT CRED_NUMERO, MOV_DATAHORA FROM APOIO.LOG_CREDENCIAL_APOIO A WHERE A.CRED_NUMERO = 10734568 AND TRUNC(MOV_DATAHORA) = TRUNC(SYSDATE)";
$conn = oci_connect('xxx', 'xxx', '127.0.0.1/DBUAM');
$dados = oci_parse($conn, $sql);
oci_execute($dados, OCI_DEFAULT);

$linhas = array();
while($l = oci_fetch_assoc($dados)){
    $linhas[] = $l;
}

$json = (json_encode($linhas));
echo $json;
?>

The result of json is this:

[{"CRED_NUMERO":"10734568","MOV_DATAHORA":"26-AUG-15"},{"CRED_NUMERO":"10734568","MOV_DATAHORA":"26-AUG-15"}]

On the table it’s like this:
Retorno SQL oracle via PL/SQL

If you notice the result of JSON shows only the DATE and what matters most to me is time. How to format?

  • I think your problem is more related to the database you are using and your SQL query. You can tell us what your database is?

  • The database used is Oracle

  • In select, I believe you need to format the date different from the default, doc

  • @Victorsued Testa switch TRUNC(SYSDATE) for TO_DATE(SYSDATE, 'dd/MM/RRRR HH:mm:ss')

1 answer

4


use

$sql ="SELECT CRED_NUMERO, TO_CHAR(MOV_DATAHORA,'HH24:MI:SS') MOV_DATAHORA FROM APOIO.LOG_CREDENCIAL_APOIO A WHERE A.CRED_NUMERO = 10734568 AND TRUNC(MOV_DATAHORA) = TRUNC(SYSDATE)"

If you want to recover only the time or replace to

TO_CHAR(MOV_DATAHORA,'DD/MM/YYYY HH24:MI:SS')

To get the date and time. The default in the PHP settings must be in another format

  • Finally someone who knows what you’re talking about, it worked out very well thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.