用于显示服务器上的 javascript 时间的脚本不起作用
Script to show javascript time on server not working
我试图在我的页面上显示一个实时的javascript时钟,它显示了服务器上的时间。几周前,我在互联网上发现了这段代码,但它似乎并没有做它声称要做的事情。它显示客户端时间,而不是服务器时间。知道为什么吗?
flag = true;
timer = '';
setInterval(function(){phpJavascriptClock(<?php echo time(); ?>);},1000);
function phpJavascriptClock(timestamp)
{
if ( flag ) {
timer = timestamp * 1000;
}
var d = new Date(timer);
var currentDate = d.getDate();
currentDate = currentDate < 10 ? '0'+currentDate : currentDate;
var hours = d.getHours();
var minutes = d.getMinutes();
var seconds = d.getSeconds();
var ampm = hours >= 12 ? 'PM' : 'AM';
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 + ' ' + ampm;
var output = '';
output +=
'<a>'
+ '<i class="ace-icon fa fa-clock-o"></i>'
+ '<span class="">' + strTime + ' (SA)</span>'
+ '</a>'
;
document.getElementById("current_time").innerHTML = output ;
flag = false;
timer = timer + 1000;
}
我看到这 4 个 Javascript DATE
的构造函数:
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
可能是您的时间戳不milliseconds
或dateString
,因此 Javascript 只是创建一个没有任何参数的新Date()
对象(然后是客户端时间)?
编写一个新的基于 jQuery 的时钟。尽管它每分钟轮询一次服务器,但它可以工作。
我的控制器打发时间:
public function getServerTimeAction() {
global $config;
try{
$return["time"] = date("H:i a");
$return["timezone"] = $config->application->timezone;
$this->returnJson( JsonReturnObject::success( $return ) ) ;
} catch ( Exception $e ) {
$this->returnJson( JsonReturnObject::error( $e ) );
}
}
jquery代码:
function getServerTime() {
$.ajax({
url : "{{ url('json/session/get-server-time') }}",
cache : false,
success : function ( data ) {
if ( data.success ) {
var output = '';
output +=
'<a>'
+ '<i class="ace-icon fa fa-clock-o"></i>'
+ '<span class="">' + data.data.time + ' (SA)</span>'
+ '</a>'
;
$("#current_time").html(output);
setTimeout(getServerTime, 60000);
}
}
})
}
相关文章:
- Java脚本时间添加
- 如何根据时间运行不同的脚本
- 显示时间的脚本
- 创建一个倒计时计时器脚本,该脚本计算声音文件的持续时间,而不是特定的日期
- 我的时间倒计时脚本在谷歌浏览器中工作正常,但在其他浏览器中打开时它显示为南楠
- 时间戳日期&时间格式-应用程序脚本-设置日期和时间格式
- 在 Node.Js 脚本中使用 Javascript 计时器在特定时间触发事件
- 如何转换SalesForce Api提供的UTC日期格式,并使用Google Apps脚本将其转换为本地日期和时间格式
- 如何修复长时间运行的脚本
- 从PHP脚本获取音频文件时,更改JavaScript音频对象的当前时间
- 如何使用java脚本代码计算禁用按钮的时间
- JS脚本在浏览器控制台或FireBug控制台上运行有时间限制吗
- 编写脚本以查找页面空闲时间
- 使用java脚本获取印度标准时间
- 长时间运行的脚本IE与Chrome/Firefox
- 在同一电子表格中的多张工作表上运行基于时间的脚本
- 日期&时间脚本-ie vs ff
- 工作时间脚本 javascript
- 如何在这个js时间脚本上添加day
- 不包含时间脚本不工作