Skip to content

Convert PHP string to javascript string [closed]

For example, I have this php string: “2015-06-26”. I want to assign it to a javascript var.

I tried with String(“2015-06-26”), but it’s substracting the numbers, so the result is this: “1983”.

How am I supposed to do it? Thanks.

Answer

Use json_encode() to convert any PHP value to the corresponding Javascript literal:

var datestring = <?php echo json_encode($datestring); ?>;