Pass Datetime/Timestamp from PHP to Javascript by echo

datetime, javascript, php, timestamp

Solution

Try this:

startLive = new Date(<?php echo strtotime($start_date)*1000; ?>);

Explanation:

PHP's `strtotime` function returns a Unix timestamp (seconds since 1-1-1970 at midnight).

Javascript's `Date()` function can be instantiated by specifying milliseconds since 1-1-1970 at midnight.

So multiply seconds by 1000 and you get milliseconds, which you can use in Javascript.

Problem

How can I pass a datetime/timestamp from PHP to javascript. The following does not appear to work: ``` startLive = new Date(<?php echo date("U", strtotime($start_date)); ?>); ```

Original source