显示服务器时间,包括时区偏移
show server time including timezone offset
我在使用 php 和 js 显示服务器的实际时间时遇到一些困难。
在服务器端我有以下 php 代码:
$date = new DateTime();
echo $date->getTimestamp();
客户端如果有如下js代码改变div的内容显示当前时间:
flag = true;
timer = '';
function clock()
{
if ( flag ) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "backend/time_backend.php?action=serverTime", false);
xmlhttp.send();
var stamp = xmlhttp.responseText;
timer = stamp*1000;
}
var d = new Date(timer);
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
//hours = hours % 12;
//hours = hours ? hours : 12; // the hour ’0' should be ’12'
minutes = minutes < 10 ? '0'+minutes : minutes;
seconds = seconds < 10 ? '0'+seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds;
document.getElementById("clock").innerHTML= strTime ;
flag = false;
timer = timer + 1000;
}
window.onload = function() {
setInterval(clock, 1000);
};
只要服务器的时区和我的时区相同,这就有效。但是一旦我更改服务器上的时区,它就不再起作用了。它仍然会显示我的本地客户端时间,尽管服务器上的 bash 命令 date
以正确的偏移量显示时间。
我该如何解决这个问题?我真的需要显示服务器本地时间。
您正在从 PHP 发送 unix 时间戳
一定是在使用
echo date("Y-m-d H:i:s", time());
如果你想使用字符串来使用 JS 创建日期对象。
你的JS代码应该是
function clock()
{
if ( flag ) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "backend/time_backend.php?action=serverTime", false);
xmlhttp.send();
var stamp = xmlhttp.responseText;
var timer = new Date(stamp);
}
var d = new Date(timer);
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
//hours = hours % 12;
//hours = hours ? hours : 12; // the hour ’0' should be ’12'
minutes = minutes < 10 ? '0'+minutes : minutes;
seconds = seconds < 10 ? '0'+seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds;
document.getElementById("clock").innerHTML= strTime ;
flag = false;
timer = new Date(timer.getTime() + 1000);
}
您可以使用 ISO 8601 格式的日期。
$date = date("c", time());
给你一个。
ISO 8601 Data elements and interchange formats – Information
interchange – Representation of dates and times is an international
standard covering the exchange of date and time-related data. It was
issued by the International Organization for Standardization (ISO) and
was first published in 1988. The purpose of this standard is to
provide an unambiguous and well-defined method of representing dates
and times, so as to avoid misinterpretation of numeric representations
of dates and times, particularly when data are transferred between
countries with different conventions for writing numeric dates and
times.
然后您可以执行 Date.parse(),其中将 return 时间戳 ;) 然后像您收到时间戳一样继续操作。
Date::getTimestamp
总是returnsUnix timestamp。 Unix 时间戳不存储时区信息。
解决方案是根据服务器提供的日期信息构建 JavaScript 日期对象。
注意:当服务器或客户端更改时区(即 DST)时,时间不会不同步。如果避免请求后端,更准确的解决方案是在 JavaScript(例如 timezone.js)中使用时区库。
PHP:
$date = new DateTime;
echo json_encode(array(
'year' => (int) $date->format('Y'),
'month' => (int) $date->format('m'),
'day' => (int) $date->format('j'),
'hours' => (int) $date->format('H'),
'minutes' => (int) $date->format('i'),
'seconds' => (int) $date->format('s'),
));
JavaScript:
var date = null;
function updateTime() {
if (!date) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "backend/time_backend.php?action=serverTime", false);
xmlhttp.send();
var j = JSON.parse(xmlhttp.responseText);
date = new Date(
j['year'], j['month'],
j['day'], j['hours'],
j['minutes'], j['seconds']
);
return;
}
// Increment time by 1 second
date.setTime(date.getTime() + 1000);
}
function clock() {
updateTime();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
hours = hours % 12;
hours = hours ? hours : 12; // the hour ’0' should be ’12'
minutes = minutes < 10 ? '0'+minutes : minutes;
seconds = seconds < 10 ? '0'+seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds;
document.getElementById('clock').innerHTML = strTime;
}
window.onload = function() {
setInterval(clock, 1000);
};
我在使用 php 和 js 显示服务器的实际时间时遇到一些困难。
在服务器端我有以下 php 代码:
$date = new DateTime();
echo $date->getTimestamp();
客户端如果有如下js代码改变div的内容显示当前时间:
flag = true;
timer = '';
function clock()
{
if ( flag ) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "backend/time_backend.php?action=serverTime", false);
xmlhttp.send();
var stamp = xmlhttp.responseText;
timer = stamp*1000;
}
var d = new Date(timer);
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
//hours = hours % 12;
//hours = hours ? hours : 12; // the hour ’0' should be ’12'
minutes = minutes < 10 ? '0'+minutes : minutes;
seconds = seconds < 10 ? '0'+seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds;
document.getElementById("clock").innerHTML= strTime ;
flag = false;
timer = timer + 1000;
}
window.onload = function() {
setInterval(clock, 1000);
};
只要服务器的时区和我的时区相同,这就有效。但是一旦我更改服务器上的时区,它就不再起作用了。它仍然会显示我的本地客户端时间,尽管服务器上的 bash 命令 date
以正确的偏移量显示时间。
我该如何解决这个问题?我真的需要显示服务器本地时间。
您正在从 PHP 发送 unix 时间戳 一定是在使用
echo date("Y-m-d H:i:s", time());
如果你想使用字符串来使用 JS 创建日期对象。
你的JS代码应该是
function clock()
{
if ( flag ) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "backend/time_backend.php?action=serverTime", false);
xmlhttp.send();
var stamp = xmlhttp.responseText;
var timer = new Date(stamp);
}
var d = new Date(timer);
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
//hours = hours % 12;
//hours = hours ? hours : 12; // the hour ’0' should be ’12'
minutes = minutes < 10 ? '0'+minutes : minutes;
seconds = seconds < 10 ? '0'+seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds;
document.getElementById("clock").innerHTML= strTime ;
flag = false;
timer = new Date(timer.getTime() + 1000);
}
您可以使用 ISO 8601 格式的日期。
$date = date("c", time());
给你一个。
ISO 8601 Data elements and interchange formats – Information interchange – Representation of dates and times is an international standard covering the exchange of date and time-related data. It was issued by the International Organization for Standardization (ISO) and was first published in 1988. The purpose of this standard is to provide an unambiguous and well-defined method of representing dates and times, so as to avoid misinterpretation of numeric representations of dates and times, particularly when data are transferred between countries with different conventions for writing numeric dates and times.
然后您可以执行 Date.parse(),其中将 return 时间戳 ;) 然后像您收到时间戳一样继续操作。
Date::getTimestamp
总是returnsUnix timestamp。 Unix 时间戳不存储时区信息。
解决方案是根据服务器提供的日期信息构建 JavaScript 日期对象。
注意:当服务器或客户端更改时区(即 DST)时,时间不会不同步。如果避免请求后端,更准确的解决方案是在 JavaScript(例如 timezone.js)中使用时区库。
PHP:
$date = new DateTime;
echo json_encode(array(
'year' => (int) $date->format('Y'),
'month' => (int) $date->format('m'),
'day' => (int) $date->format('j'),
'hours' => (int) $date->format('H'),
'minutes' => (int) $date->format('i'),
'seconds' => (int) $date->format('s'),
));
JavaScript:
var date = null;
function updateTime() {
if (!date) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "backend/time_backend.php?action=serverTime", false);
xmlhttp.send();
var j = JSON.parse(xmlhttp.responseText);
date = new Date(
j['year'], j['month'],
j['day'], j['hours'],
j['minutes'], j['seconds']
);
return;
}
// Increment time by 1 second
date.setTime(date.getTime() + 1000);
}
function clock() {
updateTime();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
hours = hours % 12;
hours = hours ? hours : 12; // the hour ’0' should be ’12'
minutes = minutes < 10 ? '0'+minutes : minutes;
seconds = seconds < 10 ? '0'+seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds;
document.getElementById('clock').innerHTML = strTime;
}
window.onload = function() {
setInterval(clock, 1000);
};