如何在javascript中获取服务器时区 [英] How to get server timezone in javascript

查看:70
本文介绍了如何在javascript中获取服务器时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Javascript中设置不同的时区。目前它显示本地机器或客户端的PC日期/时区的日期和时区。

I want to set different timezone in Javascript.Currently It is showing date and time zone of local machine or client's PC date /timezone.

问候,

推荐答案

Javascript是一种客户端语言,不会以这种方式与服务器交互。您需要从服务器端平台获取该数据。

Javascript is a client-side language and does not interact with the server that way. You'll need to fetch that data from your server-side platform.

这是一些用于获取您要查找的数据的PHP代码。您需要将其放入页面并将结果回显到JS变量....

Here is some PHP code to get the data you are looking for. You'll either need to put this in your page and echo the result into a JS variable....

<?php
    $date = new DateTime(null, new DateTimeZone('Europe/London'));
    $tz = $date->getTimezone();
    $tzone = $tz->getName();
?>

<script type="text/javascript">
    var timeZone='<?php echo $tzone ?>';
</script>






....或保持PHP页面分开,并使用AJAX获取数据


....or keep the PHP page separate, and fetch the data using AJAX

getTimeZone.php

<?php
    $date = new DateTime(null, new DateTimeZone('Europe/London'));
    $tz = $date->getTimezone();
    echo $tz->getName();
?>

JS

var timeZone=null;
$.get('getTimeZone.php', function(result){
    timeZone=result;
}, 'html');
//I know this is jQuery, not JS, but you get the idea.

这篇关于如何在javascript中获取服务器时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆