Home » Php » Pass Datetime/Timestamp from PHP to Javascript by echo

Pass Datetime/Timestamp from PHP to Javascript by echo

Posted by: admin July 12, 2020 Leave a comment

Questions:

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)); ?>); 
How to&Answers:

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.

Answer:

I think that very simple and more universal solution would be

var dateTime = <?php echo date('c', strtotime($yourDateTime)) ?>;

Answer:

You can use this:

startLive = new Date("<?php echo date("F d, Y G:i:s",strtotime($start_date)); ?>");

this will sort your problem

Explanation:

Check Here