将PHP回显日期转换为客户端本地时间

fea*_*lex 1 javascript php date localtime

这是我的第一篇文章.我有一个问题似乎无法解决.在这里:

我有一个PHP脚本,可以将事件的日期打印到页面:

$event_datetime = date("g:i A (m/d/y)", strtotime($row['event_time']));
echo $event_datetime
Run Code Online (Sandbox Code Playgroud)

我想使用Javascript将$ event_datetime转换为客户端本地时区或本地客户端计算机时间.

有任何想法吗 ?

提前致谢 !

cro*_*ird 5

而不是回显日期到javascript,只需回显时间戳并使用javascripts日期对象.

var date = new Date(<?php echo strtotime($row['event_time']) * 1000); ?>);
Run Code Online (Sandbox Code Playgroud)

您现在可以使用javascripts日期对象打印出您想要的时间.像这样:

date.toLocaleDateString();
//The default output will be the users' timezone (the client where the javascript is being executed), but can be configured with the options argument
Run Code Online (Sandbox Code Playgroud)

看到:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

有关javascript日期对象的更多信息