如何独立于用户位置在 JS / jQuery 中正确显示 php 服务器时间和时区?

How to display php server time and timezone correctly in JS / jQuery independent of user location?

首先,我很抱歉提出另一个与网络相关的时间和时区问题。我知道有很多资源和类似的问题,但我仍然无法理解 php 和 js 时间之间的时间转换是如何工作的。

这是我的情况。我想将包括时区在内的服务器时间获取到 javascript 并在客户端执行某些操作(与此问题无关)。我有这个简短的片段,但它没有像我期望的那样工作:

get_date.php

<?php
date_default_timezone_set('Romania/Bucharest');
echo date('D M d Y H:i:s');
?>

index.html

<body>

  <!-- jQuery -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

  <script type='text/javascript'>

  $.get( "get_date.php", function(data) {
    current = new Date(data);
    alert(current);
  });

  </script>

</body>

提示信息

所以问题是我得到的是罗马尼亚的日期和时间(服务器的位置),但是柏林的偏移量和时区(我访问网站的位置)。我想在 Javascript、 独立于用户位置 中正确获取服务器时间、日期、偏移量和时区。例如,警报消息应为 Thu Feb 12 2015 13:26:10 GMT+0200(EET).

也欢迎澄清为什么这没有按预期工作!

改变

get_date.php

<?php
date_default_timezone_set('Romania/Bucharest');
echo date('D M d Y H:i:s');
exit();
?>

还有你的脚本文件。不要将数据传递给 jquery 的日期函数。

<script type='text/javascript'>

  $.get( "get_date.php", function(data) {
    alert(data);
  });

</script>

请分享结果。

当您使用 JavaScript 的日期功能时,您的浏览器将根据您系统的时区设置显示日期和时间。为防止这种情况,您需要在服务器上格式化日期并将其作为字符串处理。 如果出于任何原因您必须使用 javaScript 日期函数,则需要通过预先计算偏移量来更正日期。

使用 JavaScript 的原生 Date 对象是不可能的——它始终在客户端时区运行。

使用像 Moment.js and it’s extension Moment Timezone 这样的库。这些允许您在 JS 中使用您喜欢的任何时区,包括解析日期和格式化它们的可能性。

您可以使用http://json-time.appspot.com/time.json or its variants like http://json-time.appspot.com/time.json?tz=GMT

var currTime;
$.ajax({
            type: "GET", 
            dataType: 'jsonp', 
            url: "http://json-time.appspot.com/time.json",
            async: false,
            contentType: "application/json; charset=utf-8",
            success: function (msg) {
                console.log(msg);  
    // currTime.tz = "UTC"
    // currTime.hour = 12
    // currTime.datetime = "Thu, 12 Feb 2015 12:13:39 +0000"
    // currTime.minute = 13
    // currTime.second = 39              
            }
});

这是一个 PHP 脚本,您可以将其放在自己的服务器上并使用它来代替 json-time.appspot.com

<?php
header('Content-Type: application/json');
header("Expires: Tue, 01 Jan 1990 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$error = "false";
$tz = $_GET['tz'];

if ( !in_array($tz, DateTimeZone::listIdentifiers())) {
   $error = 'invalid time zone';
   $tz = 'UTC';
}

date_default_timezone_set($tz);

?>
<?php echo htmlspecialchars($_GET['callback'], ENT_QUOTES, 'UTF-8' ); ?>({
 "tz": "<?php echo $tz ?>",
 "hour": <?php echo date('G'); ?>,
 "datetime": "<?php echo date(DATE_RFC2822); ?>",
 "second": <?php echo intval(date('s')); ?>,
 "error": "<?php echo $error; ?>",
 "minute": <?php echo intval(date('i')); ?>
})

在 PHP 中使用以下代码。我在我的项目中使用相同的代码来获得正确的服务器时间

 <?php 
  date_default_timezone_set('Romania/Bucharest');  
  echo date('F d,Y h:i:s');
  die();

?>

 <body>

 <!-- jQuery -->
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

 <script type='text/javascript'>

 $.get( "get_date.php", function(data) {
    current = new Date(data);
   alert(current);
 });

 </script>

请参阅 w3school javascript 数据函数以使用字符串

获取正确的日期