timerset.php。這個文件創建服務器端的文件,以推動完成時間。
<?php
$year=htmlspecialchars($_GET['year']);
$month=htmlspecialchars($_GET['month']);
$day=htmlspecialchars($_GET['day']);
$hour=htmlspecialchars($_GET['hour']+2);//NBNBNB the +2 changes according to YOUR timezone.
$minute=htmlspecialchars($_GET['minute']);
$second=htmlspecialchars($_GET['second']);
$timestring=$year.":".$month.":".$day.":".$hour.":".$minute.":".$second;
echo $timestring."<br/>";
$filename = 'timerserver.php';
$file = fopen($filename, 'w');
rewind($file);
fwrite($file,"<?php\n");
fwrite($file, "header('Content-Type: text/event-stream');\n");
fwrite($file, "header('Cache-Control: no-cache');\n");
fwrite($file, "header('connection:keep-alive');\n");
fwrite($file, "\$starttime=\"$timestring\";\n");
fwrite($file, "echo \"data:{\$starttime}\\n\\n\";\n");
fwrite($file, "ob_flush();");
fwrite($file,"flush();");
fwrite($file,"sleep(3);");
fwrite($file,"?>\n");
fflush($file);
ftruncate($file, ftell($file));
fclose($file);
flush();
?>
timer.php。該文件接收完成時間並更新顯示的時間。
<html>
<header>
</header>
<body>
<script>
var source = new EventSource("timerserver.php");
var mystarttime="00:00";
source.onmessage = function(event)
{
mystarttime=event.data;
};
setInterval(function() {test(mystarttime)}, 1000);
function test(intime)
{
var timestring=intime;
var split = timestring.split(':');
var currentdate=new Date();
var finishtime=new Date(split[0],split[1]-1,split[2],split[3],split[4],split[5],0);
var timediff=new Date(finishtime-currentdate);
var year=timediff.getUTCFullYear();
var minutes=timediff.getUTCMinutes();
var seconds=timediff.getUTCSeconds();
var currentMinutes = ("0" + minutes).slice(-2);
var currentSeconds = ("0" + seconds).slice(-2);
if (year<1970)
{
document.getElementById("result").innerHTML="Time is up"
}
else
document.getElementById("result").innerHTML = currentMinutes+":"+currentSeconds;
}
</script>
<div id="result">Fetching time...</div>
</body>
</html>
timerform.php。一種更新時間的方法。
<?php
echo "<b>NB : Time is in UTC Timezone</b><br/><hr/>";
date_default_timezone_set("UTC");
$currentdatetime=date("Y-m-d H:i:s");
$year=date("Y");
$month=date("m");
$day=date("j");
$hour=date("H");
$minute=date("i");
$second=0;
echo $currentdatetime."<hr/>";
?>
<form action="timerreset.php" method="GET">
Year <input name="year" value="<?php echo $year;?>"/><br/>
Month <input name="month" value="<?php echo $month;?>"/><br/>
Day <input name="day" value="<?php echo $day;?>"/><br/><br/>
Hour <input name="hour" value="<?php echo $hour;?>"/><br/>
Minute <input name="minute" value="<?php echo $minute+1;?>"/><br/>
Second <input name="second" value="<?php echo $second;?>"/><br/>
<input type="submit"/>
</form>
<?php
flush();
?>
NB,使用UTC時間,以滿足在不同的時區不同的客戶端。
我有上面的代碼在我的本地主機(Windows)和SouthCoastHosting(Linux)上工作,所以讓我知道你是否有任何問題。
要使用它打開一個選項卡到southcoasthosting.com/timer/timerform.php和另一個選項卡到southcoasthosting.com/timer/timer.php。使用計時器窗體來更改結束時間,您將在timer.php中看到時間變化。由於EventSource的性質,在更新定時器之前總是會有最多3秒的延遲。根據您的客戶端與服務器的連接速度,可能會有進一步的延遲。我選擇發送完成時間,以便客戶不會因這些延遲而滯後。
我希望一切都有道理。否則讓我知道。
爲什麼不重新發送過期時間戳,那麼你的客戶可以做數學計算,不管何時它意識到需要重置,它可以基於JavaScript中的本地時鐘設置計時器。 – Andy 2014-10-30 12:27:51