Reputation: 4371
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)); ?>);
Upvotes: 7
Views: 23405
Reputation: 391
I think that very simple and more universal solution would be
var dateTime = <?php echo date('c', strtotime($yourDateTime)) ?>;
Upvotes: 2
Reputation: 14479
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.
Upvotes: 30
Reputation: 1402
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:
Upvotes: 0