1

When using CONVERT(VARCHAR(10),datetime,111) in my SQL query and outputting to php I get the date back in the following format.

2015\/08\/03

Is there a reason why there are the additional slashes?

My php code:

<?php
    $sql = "SELECT id, custcode, title, description, CONVERT(VARCHAR(10),datetime,111) as 'date', status
              FROM dbo.calls";

    $stmt=sqlsrv_query($conn, $sql);

    // Initializes a container array for all of the calendar events
    $jsonArray = array();

    while($row = sqlsrv_fetch_array($stmt)) {

        // Stores each database record to an array
        $buildjson = array('title' => $row['custcode'], 'start' => $row['date'], 'allday' => false);

        // Adds each array into the container array
        array_push($jsonArray, $buildjson);
    }

    // Output the json formatted data so that the jQuery call can read it
    echo json_encode($jsonArray);
?>

thanks

C1pher
  • 1,933
  • 6
  • 33
  • 52
Dev.W
  • 2,340
  • 9
  • 38
  • 77
  • 1
    the problem is in PHP code, provided code does not show the issue area, can you please update your question with the php code as well – M.Ali Aug 04 '15 at 22:12
  • @M.Ali just added the php script minus the $conn etc – Dev.W Aug 04 '15 at 22:14
  • @maraca oh jees.. could you explain in more detail or show in my code what I should do to change? – Dev.W Aug 04 '15 at 22:29
  • @maraca ive got a php page which calls the fullcalendar js file which within the function calls a JSON array which is the php script above.. when outputting just the php script directly I get.. [{"title":"test1","start":"2015\/08\/03","allday":false},{"title":"test2","start":"2015\/08\/04","allday":false}] So the scripts working, however the receiver needs the date in the following format YYYY/MM/DD but for some reason my code is adding the additional \ – Dev.W Aug 04 '15 at 22:40

1 Answers1

0

I just verified that json_encode does escape / in Strings so that it becomes \/. It would still be valid JSON without escaping them, but php does it anyway.

You should use a JSON parser (e.g. jQuery $.parseJSON(...)) to get an object from it or you can just replace \/ by /.

Or see here how to Parse JSON in JavaScript?

Community
  • 1
  • 1
maraca
  • 8,468
  • 3
  • 23
  • 45