Pass Datetime/Timestamp from PHP to Javascript by echo

22,325

Solution 1

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.

Solution 2

I think that very simple and more universal solution would be

var dateTime = <?php echo date('c', strtotime($yourDateTime)) ?>;
Share:
22,325
Ben
Author by

Ben

...

Updated on March 17, 2020

Comments

  • Ben
    Ben about 4 years

    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)); ?>);