2

Possible Duplicate:
Convert date format yyyy-mm-dd => dd-mm-yyyy

I am extracting data from MySQL and passing it to a PHP page, part of which is a Javascript timeline graph.

I am using json_encode to convert the query result into a format that the Javascript timeline requires.

However json_encode outputs the following:

{"start":"2012-06-06 18:05:21", "content":"Start Date", }

Whereas the format the timeline needs is

{"start": new Date(2010,7,23,23,0,0), 'content': "Start Date", }

Can I convert the output to the required format either before, or as part of the json_encode process?

Community
  • 1
  • 1
highfidelity
  • 107
  • 1
  • 3
  • 9
  • You can use php strtotime() function, but don't forget you will get timestamp in seconds whereas javascript uses millisecond timestamp, so you can multiply result by 1000. – rock-ass Dec 28 '12 at 14:29
  • 2
    **NEVER USE** `strtotime()` if `DateTime::createFromFormat()` is available. – Paul T. Rawkeen Dec 28 '12 at 14:40

3 Answers3

4

Generally speaking, you'll be best off doing this in PHP after you pull the value from the database.

See the answer given Convert date format yyyy-mm-dd => dd-mm-yyyy and adjust the parameters according to http://php.net/date.

You won't be able to do exactly what you want though, as json_encode() will always wrap values in quotes and there is no way to have a native JavaScript statement in the data.

Your best bet will be converting the date into a normal string with the format Month Day, Year Hours:Minutes:Seconds.

New Date() can understand that format - then just do a New Date(data.start) in JavaScript after pulling the JSON.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Thanks, I tried this but ran into the problem you outlined - json_encode wraps the values in quotes which means I cant use it without further manipulation. So I have abandoned json_encode and just put the dates and instructions into an array. I used the code provided by Paul Rawkeen below, so I will accept his answer, hope you dont mind :) – highfidelity Dec 28 '12 at 15:20
  • @high no problem - but neither my nor Paul's suggestion makes it necessary to abandon json_encode()? Just pass the date as a normal string and do the `New Date()` in JavaScript. If you put it in `M D,Y H:i:s` format, you don't even need to split it on JS side – Pekka Dec 28 '12 at 15:22
3
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2012-06-06 18:05:21'); // your original DTO
$newFormat = $date->format('Y,m,d,H,i,s'); // your newly formatted date ready to be substituted into JS new Date();
  unset($date);

$json = json_encode(["start" => $newFormat, 'content' => "Start Date"]);

In JS you can do smth. similar to:

var objectFromJSON = some_json_decode_procedure(); // decoding JSON to native object
var dateArray = objectFromJSON.start.split(','); // splitting string to elements for new Date()
objectFromJSON.start = new Date(dateArray[0], dateArray[1], dateArray[2], dateArray[3], dateArray[4], dateArray[5]); // resetting Date() object in the object
Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
  • See comments above, but using a combination of Pekka's instructions and your code I got this work with an array as opposed to json_encode. Thanks for the help – highfidelity Dec 28 '12 at 15:21
  • Why not encode the date in a way that `new Date()` can understand without having to split it? (`y m d, h:i:s`) – Pekka Dec 28 '12 at 15:23
  • @Pekka, It is just the way of implementation. Maybe not the simplest one, but working :). Pre-formatted date is also Ok, but using this approach we can use some of transmitted `date` elements somewhere later, but it doesn't really matter bec. we can extract it from `Date()` object in JS. :) – Paul T. Rawkeen Dec 28 '12 at 15:35
0

You can create date like

var date = new Date("2012-06-06 18:05:21"); 

then try following

data.addRow([new Date(date.getYear(),date.getMonth(),date.getUTCDate())]);
Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
Avinash T.
  • 2,280
  • 2
  • 16
  • 23