Skip Datetime / Timestamp from PHP to Javascript from echo

How to pass date and time from PHP to javascript. The following does not work:

startLive = new Date(<?php echo date("U", strtotime($start_date)); ?>); 
+6
source share
3 answers

Try the following:

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

Explanation:

The PHP strtotime function returns a Unix timestamp (seconds 1-1 to 1970 at midnight).

A Javascript Date() function can be created by indicating milliseconds from July 1 to July 19 at midnight.

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

+26
source

I think a very simple and universal solution would be

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

You can use this:

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

it sorts your problem

Explanation:

Check here

0
source

Source: https://habr.com/ru/post/913641/


All Articles