创建 php 个实时时钟
create php live clock
我如何使用 php 创建一个实时时钟,它从服务器而不是用户电脑时间获取时间 [不是 javascript]
我使用了下面的代码,但是当使用 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>
PHP是服务器端编程语言,Javascript是客户端编程语言。
填充变量的 PHP 代码只会在网页加载时更新,之后您只剩下 Javascript 代码,仅此而已。
我建议您搜索一本基础编程书籍,其中提到了诸如客户端和服务器端代码之类的概念,因为(并不想刻薄)您似乎对这些东西的工作原理有很大的误解。
您可以使用 ajax
.
timestamp.php
<?php
date_default_timezone_set('YOUR TIMEZONE');
echo $timestamp = date('H:i:s');
jQuery
$(document).ready(function() {
setInterval(timestamp, 1000);
});
function timestamp() {
$.ajax({
url: 'http://localhost/timestamp.php',
success: function(data) {
$('#timestamp').html(data);
},
});
}
HTML
<div id="timestamp"></div>
我会从服务器发送时间戳,只是为了获取当前服务器时间的快照,然后让 JS 从那里接管。 JS 可以使本地时钟非常接近于与您的服务器同步,并且您可以 运行 一个新的 ajax 调用每 x 个 minutes/hours 以重新同步新的时间戳。
我还没有对此进行测试,但是如果准确性不是问题,这应该会很好地工作并且将您的服务器工作量保持在最低限度。
编辑:实际上我最终为我正在从事的项目做了这个并且效果很好。
您只需要将您的时间戳从您的服务器获取到您的 JS - 因为我的页面是 .php 页面我可以这样做:
<h1 id="current-time-now" data-start="<?php echo time() ?>"></h1>
然后我可以获取该时间戳并像这样使用它:
//get new date from timestamp in data-start attr
var freshTime = new Date(parseInt($("#current-time-now").attr("data-start"))*1000);
//loop to tick clock every second
var func = function myFunc() {
//set text of clock to show current time
$("#current-time-now").text(freshTime.toLocaleTimeString());
//add a second to freshtime var
freshTime.setSeconds(freshTime.getSeconds() + 1);
//wait for 1 second and go again
setTimeout(myFunc, 1000);
};
func();
从那里您可以 运行 ajax 调用以获取新的时间戳,无论您认为项目需要多久。
我如何使用 php 创建一个实时时钟,它从服务器而不是用户电脑时间获取时间 [不是 javascript]
我使用了下面的代码,但是当使用 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>
PHP是服务器端编程语言,Javascript是客户端编程语言。
填充变量的 PHP 代码只会在网页加载时更新,之后您只剩下 Javascript 代码,仅此而已。
我建议您搜索一本基础编程书籍,其中提到了诸如客户端和服务器端代码之类的概念,因为(并不想刻薄)您似乎对这些东西的工作原理有很大的误解。
您可以使用 ajax
.
timestamp.php
<?php
date_default_timezone_set('YOUR TIMEZONE');
echo $timestamp = date('H:i:s');
jQuery
$(document).ready(function() {
setInterval(timestamp, 1000);
});
function timestamp() {
$.ajax({
url: 'http://localhost/timestamp.php',
success: function(data) {
$('#timestamp').html(data);
},
});
}
HTML
<div id="timestamp"></div>
我会从服务器发送时间戳,只是为了获取当前服务器时间的快照,然后让 JS 从那里接管。 JS 可以使本地时钟非常接近于与您的服务器同步,并且您可以 运行 一个新的 ajax 调用每 x 个 minutes/hours 以重新同步新的时间戳。
我还没有对此进行测试,但是如果准确性不是问题,这应该会很好地工作并且将您的服务器工作量保持在最低限度。
编辑:实际上我最终为我正在从事的项目做了这个并且效果很好。
您只需要将您的时间戳从您的服务器获取到您的 JS - 因为我的页面是 .php 页面我可以这样做:
<h1 id="current-time-now" data-start="<?php echo time() ?>"></h1>
然后我可以获取该时间戳并像这样使用它:
//get new date from timestamp in data-start attr
var freshTime = new Date(parseInt($("#current-time-now").attr("data-start"))*1000);
//loop to tick clock every second
var func = function myFunc() {
//set text of clock to show current time
$("#current-time-now").text(freshTime.toLocaleTimeString());
//add a second to freshtime var
freshTime.setSeconds(freshTime.getSeconds() + 1);
//wait for 1 second and go again
setTimeout(myFunc, 1000);
};
func();
从那里您可以 运行 ajax 调用以获取新的时间戳,无论您认为项目需要多久。