Ben
Ben

Reputation: 4371

Pass Datetime/Timestamp from PHP to Javascript by echo

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

Answers (3)

Javlonbek
Javlonbek

Reputation: 391

I think that very simple and more universal solution would be

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

Upvotes: 2

Travesty3
Travesty3

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

Vipin Jain
Vipin Jain

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:

Check Here

Upvotes: 0

Related Questions