pato.llaguno
pato.llaguno

Reputation: 741

json enconde just date value

This is just a matter of knowing if this is possible, i am making a CSV file from a sql query executed via a php via called thru JS, this query has some dates on it, so when i json encode them to return to javascript to make the actual CSV file i get these dates as [object Object]. My question is, is there a way to json_encode this with the value and not the whole date object, or is the problem in the way my date is returned from the query?

this is my php file.

<?php
include "ChromePhp.php";
//ChromePhp::log('HOLA');
$inicio =($_POST['inicio']);
$fin =($_POST['fin']);
$agente = isset($_POST['AgR']) ? strval($_POST['AgR']) : '' ;
if($agente == 'Selecciona...'){
    $agente = '';
}
$date = date("Y-m-d H:i:s");
//ChromePhp::log($inicio);
//ChromePhp::log($fin);
//ChromePhp::log($agente);

include "includes/db_config.php";
$conn = sqlsrv_connect(SV_NAME, $connectionInfo) OR die("Unable to connect     to the database");

$sql = "SELECT
ISNULL(DateDiff(dd, date, resuelto),DateDiff(dd, date, GETDATE()))      As Ndays, 
ISNULL(DateDiff(hh, date, resuelto),DateDiff(hh, date, GETDATE())) % 24 As Nhours,
ISNULL(DateDiff(mi, date, resuelto),DateDiff(mi, date, GETDATE())) % 60 As Nmins,
CAST(ISNULL(DateDiff(hh, date, resuelto),DateDiff(hh, date, GETDATE())) %24     AS float)/24 + CAST(ISNULL(DateDiff(mi, date, resuelto),DateDiff(mi, date, GETDATE())) %60 AS float)/60 + CAST(ISNULL(DateDiff(dd, date, resuelto),DateDiff(dd, date, GETDATE())) AS float) AS total,
servicio.id,
servicio.date,
swo,
usuarios.nombre,
cliente,
resuelto,
flotas.ownerName,
input.form,
 CAST(REPLACE(CAST(comentariosCliente AS NVARCHAR(MAX)),CHAR(10),' ') AS NTEXT) as comentariosCliente,
CAST(REPLACE(CAST(comentariosPropios AS NVARCHAR(MAX)),CHAR(10),'') AS NTEXT) as comentariosAgente,
Areas.Area
FROM servicio left join flotas ON servicio.flota=flotas.ownerId 
left join usuarios ON servicio.[user]=[dbo].usuarios.id 
left join input on servicio.inputForm=input.id 
left join Areas ON servicio.status =Areas.id
WHERE Area like '%$agente%'
AND resuelto is not null
AND date between '$inicio' and '$fin'
order by servicio.id";
//ChromePhp::log($sql); 
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$result = sqlsrv_query($conn, $sql, $params, $options);
if( $result === false ) {
     echo "Error in executing query.</br>";
 die( print_r( sqlsrv_errors(), true));
}
$json = array();

do {
     while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
     $json[] = $row;
     }
} while ( sqlsrv_next_result($result) );

/* Run the tabular results through json_encode() */
/* And ensure numbers don't get cast to trings */
echo json_encode($json);
//ChromePHP::log($json);
/* Free statement and connection resources. */
sqlsrv_free_stmt( $result);
sqlsrv_close( $conn);
?>

Upvotes: 1

Views: 68

Answers (1)

pato.llaguno
pato.llaguno

Reputation: 741

So, i figured out that a simple convertion was enough

Convert(VARCHAR, servicio.date,120) as date,
Convert(VARCHAR, resuelto,120) as resuelto,

Upvotes: 1

Related Questions