Pass Datetime/Timestamp from PHP to Javascript by echo(通过回显将日期时间/时间戳从 PHP 传递到 Javascript)
问题描述
如何将日期时间/时间戳从 PHP 传递到 javascript.以下似乎不起作用:
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)); ?>);
推荐答案
试试这个:
startLive = new Date(<?php echo strtotime($start_date)*1000; ?>);
解释:
PHP 的 strtotime 函数返回一个 Unix 时间戳(从 1-1-1970 年午夜).
PHP's strtotime function returns a Unix timestamp (seconds since 1-1-1970 at midnight).
Javascript的Date()函数可以通过指定毫秒来实例化 自 1970 年 1 月 1 日午夜开始.
Javascript's Date() function can be instantiated by specifying milliseconds since 1-1-1970 at midnight.
所以将秒数乘以 1000 得到毫秒数,可以在 Javascript 中使用.
So multiply seconds by 1000 and you get milliseconds, which you can use in Javascript.
这篇关于通过回显将日期时间/时间戳从 PHP 传递到 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过回显将日期时间/时间戳从 PHP 传递到 Javascript
基础教程推荐
- php中的PDF导出 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的foreach复选框POST 2021-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
