sm13294
sm13294

Reputation: 563

How to treat json date as a date instead of string?

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?

Upvotes: 0

Views: 1355

Answers (1)

Mike Brant
Mike Brant

Reputation: 71384

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.

Upvotes: 4

Related Questions