0

I am creating a json array with dates in there.

$result[] = array('startDate' => date("D M j G:i:s T Y"),
                            'endDate' => date("D M j G:i:s T Y"),
                            'title' => $event->title,
                            'description'=>$event->summary,
                            'priority'=>2);

The result i get is:

 {
startDate: "Wed Nov 21 0:55:44 UTC 2012",
endDate: "Wed Nov 21 0:55:44 UTC 2012",
title: "title",
description: "desc",
priority: 2
}

But i dont want the date to be quoted as string, what i need is this:

 {
startDate: Wed Nov 21 0:55:44 UTC 2012,
endDate: Wed Nov 21 0:55:44 UTC 2012,
title: "title",
description: "desc",
priority: 2
}

So is there a way that the date to not be treated as a string?

nickhar
  • 19,981
  • 12
  • 60
  • 73
sm13294
  • 563
  • 7
  • 23
  • How are you generating the JSON string from the array? –  Nov 21 '12 at 01:16
  • i use the json_encode function – sm13294 Nov 21 '12 at 01:19
  • 2
    Isn't the point of quoting strings to ensure the JSON is well formed? For instance if the string is not quoted and you happen to have a comma in it JSON will be interpreted as ending that value there. and starting a new one, thus breaking the code. I don't see why you need it unquoted. Can you elaborate on your reasons? – kittycat Nov 21 '12 at 01:20
  • i am using this library http://www.dpereyra.com/scripts/dp_calendar/demo/events.html and the date required shouldnt be quoted. The challenge here is that i have to dynamically fill the json from php – sm13294 Nov 21 '12 at 01:22
  • 1
    There are no date values in [JSON](http://json.org/). What you want is not JSON. Your question does not make sense. – PointedEars Nov 21 '12 at 01:22
  • check my previous comment, is there a way to handle this, because as i realized the quoted string is the problem – sm13294 Nov 21 '12 at 01:24
  • Please learn about JSON before you use it. PHP generates a JSON string literal here where, if you evaluate it client-side, such as with `JSON.parse(…)`, the string value itself will not be quoted. – PointedEars Nov 21 '12 at 01:27
  • I had a quick look at the link you provided. The calendar is a JQuery application and I can't find any references to JSON at all (in the documentation). However, even if you quote the JSON data (as cryptic states), the quotes are a part of the JSON "protocol" and stripped away when you read the JSON. – Max Kielland Nov 21 '12 at 01:43

1 Answers1

4

There is no DATE value in JSON. The only allowable "value" constructs in JSON are:

  • Objects
  • Arrays
  • Double-quoted strings
  • Numbers (integer or float)
  • Boolean (true or false without quotes)
  • null

So what you are asking for is not JSON format. You would need to build your own JSON-like serializer, or modify the string after json_encode is performed to remove the double quotes.

Or, you can just leave the JSON as it is, and use javascript to parse the string into a javascript Date object, which is what appears to be the intended input to that library.

See this post about how to parse strings into Date objects in javascript.

Difference between Date(dateString) and new Date(dateString)

Note: you will likely need to change your date format as what you have is not one of the typically accepted javascript dateString formats.

Community
  • 1
  • 1
Mike Brant
  • 70,514
  • 10
  • 99
  • 103