【发布时间】:2014-02-11 17:08:21
【问题描述】:
javascript:
<script src="<?= URL::site('/public/js/jstz-1.0.4.min.js'); ?>" type="text/javascript"></script>
<script>
$(document).ready(function() {
var tz = jstz.determine(); // Determines the time zone of the browser client
});
</script>
php:
<?php
$timezone = '<script>
var tz = jstz.determine();
document.write(tz.name());
</script>';
?>
<?php
echo $timezone;
$tz = new DateTimeZone($timezone);//error producing line
$message_date = new DateTime($message_date);
$message_date->setTimeZone($tz);
$messagedate = $message_date->format('Y-m-d H:i:s');
?>
上面是获取时区的 javascript。我通过脚本获取时区并将其分配给 php 变量。如果我回显 php 变量(echo $timezone),我得到正确的时区。但如果我通过时区转换代码中的相同变量(请参阅error producing line),我收到此错误“Exception [ 0 ]: DateTimeZone::__construct(): Unknown or bad timezone (<script> var tz = jstz.determine(); document.write(tz.name()); </script>)”。
实际上,它是将整个$timezone 变量作为字符串而不是变量。需要解决方案来解决这个问题。
谢谢
【问题讨论】:
-
你喜欢使用 Ajax 吗?
-
@SumanBogati 如何使用 ajax 处理
-
简单的方法是将变量写入隐藏的 dom 元素。
-
document.write?真的...不要那样做 -
@user2681579 抱歉误会了。
标签: javascript php kohana