创建 php 实时时钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36965948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create php live clock
提问by Arash Yazdani
How can i create a live clock with php that gets time from server not users pc time [not javascript]
我怎样才能用 php 创建一个实时时钟,从服务器而不是用户 pc 时间获取时间 [不是 javascript]
i used the below code but time stops when using php variable
我使用了下面的代码,但使用 php 变量时时间停止了
<form name="Tick">
<input type="text" size="12" name="Clock">
</form>
<script type="text/javascript">
function show(){
var hours="<?php echo $myhour; ?>"
var minutes="<?php echo $mymin; ?>"
var seconds="<?php echo $mysec; ?>"
var dn="AM"
if (hours>12){
dn="PM"
hours=hours-12
//this is so the hours written out is in 12-hour format, instead of the default //24-hour format.
}
if (hours==0)
hours=12
//this is so the hours written out when hours=0 (meaning 12a.m) is 12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
document.Tick.Clock.value=
hours+":"+minutes+":"+seconds+" "+dn
setTimeout("show()",1000)
}
show()
</script>
回答by Tirth Patel
You can use ajax
.
您可以使用ajax
.
timestamp.php
时间戳.php
<?php
date_default_timezone_set('YOUR TIMEZONE');
echo $timestamp = date('H:i:s');
jQuery
jQuery
$(document).ready(function() {
setInterval(timestamp, 1000);
});
function timestamp() {
$.ajax({
url: 'http://localhost/timestamp.php',
success: function(data) {
$('#timestamp').html(data);
},
});
}
HTML
HTML
<div id="timestamp"></div>
回答by Terseus
PHP is a server-side programming language, Javascript is a client-side programming language.
PHP 是一种服务器端编程语言,Javascript 是一种客户端编程语言。
The PHP code that fills the variables will only update when the webpage is loaded, after that you are left with Javascript code and nothing more.
填充变量的 PHP 代码只会在网页加载时更新,之后只剩下 Javascript 代码,仅此而已。
I recommend you to search a basic programming book which mentions concepts such as client-side and server-side code because (and not trying to be harsh) you seems to have a big misunderstanding about how those things works.
我建议您搜索一本基本的编程书籍,其中提到了诸如客户端和服务器端代码之类的概念,因为(并且不要刻薄)您似乎对这些事情的工作方式有很大的误解。