通过回声将日期时间/时间戳从 PHP 传递到 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10233080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass Datetime/Timestamp from PHP to Javascript by echo
提问by Ben
How can I pass a datetime/timestamp from PHP to javascript. The following does not appear to work:
如何将日期时间/时间戳从 PHP 传递到 javascript。以下似乎不起作用:
startLive = new Date(<?php echo date("U", strtotime($start_date)); ?>);
回答by Travesty3
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).
PHP 的strtotime
函数返回一个 Unix 时间戳(自 1-1-1970 午夜以来的秒数)。
Javascript's Date()
function can be instantiated by specifying millisecondssince 1-1-1970 at midnight.
Javascript 的Date()
函数可以通过指定自 1-1-1970 午夜以来的毫秒数来实例化。
So multiply seconds by 1000 and you get milliseconds, which you can use in Javascript.
因此,将秒乘以 1000,您将得到毫秒,您可以在 Javascript 中使用它。
回答by Javlonbek
I think that very simple and more universal solution would be
我认为非常简单和更通用的解决方案是
var dateTime = <?php echo date('c', strtotime($yourDateTime)) ?>;
回答by Vipin Jain
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:
解释: