Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to output javascript date object from a php json_encode call [duplicate]

$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!

like image 650
Fostah Avatar asked Sep 15 '26 08:09

Fostah


2 Answers

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

like image 114
Jorge Guberte Avatar answered Sep 16 '26 22:09

Jorge Guberte


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']);
like image 40
sg3s Avatar answered Sep 16 '26 23:09

sg3s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!