Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
$array = array('start'=>"new Date(".date("Y",strtotime($start_date)).")");
$myJson = json_encode($array);

The returned json is a string, how can i convert the dates to objects? I'm using the jquery calendar plugin and it wants a date object. Thanks!

share|improve this question

2 Answers

up vote 3 down vote accepted

Send a date string normally with json and then parse it with javascript: var d = Date.parse("Jul 8, 2005");

Reference: http://www.w3schools.com/jsref/jsref_parse.asp

share|improve this answer
This is great to know! I used a few str_replaces() to just strip the quotes out of the json output but next time I will remember this. Thanks! – Fostah Aug 17 '11 at 23:17
2  
You referenced w3schools. W3schools is a terrible resource as it teaches syntax and not design. W3schools would recommend you send a js snippet and then eval(snippet) it.... -1 – sg3s Aug 17 '11 at 23:30
@sg3s It was the first result on the search... – Jorge Aug 18 '11 at 11:24
@Jorge doesn't automatically make it right. – sg3s Aug 18 '11 at 16:47
2  
@sg3s Does the solution work? – Jorge Aug 23 '11 at 13:49
show 2 more comments

Instead of trying to send javascript to the browser send just a unix timestamp and make a date with that on the server

PHP

echo json_encode(array('start' => mktime(date("Y",strtotime($start_date)))));

JS

var val = JSON.parse(json);
var date = new Date(val['start']);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.