壞消息是PHP不能強迫瀏覽器做任何事情。你需要使用Ajax來實現這一點。但是如果javascript在客戶端被禁用了呢?在php中有一個小竅門。
這裏是你可以做什麼。
確定的秒數爲直播做
$numOfSeconds = $starttime - $currtime;
然後,一旦你知道你要多少秒刷新瀏覽器在這裏是你如何可以用PHP做後啓動。
header("refresh:$numOfSeconds;urltoanywhere.php");
這將帶你到任何你想在特定時間後去的網址。如果您想刷新,請將網址定義到同一頁面。你可以擴展這個邏輯來實現你想要的方式。
UPDATE1:
下面是我所測試的工作代碼。希望這可以幫助你:)
<?php
ob_start();
date_default_timezone_set('Europe/London');
$currentPage = $_SERVER['PHP_SELF'];
$currentTimestamp = strtotime(date('Y-m-d H:i:s'));
//Define your startTime here in {Year-Month-Day Hour:Minute:Second} format
$startTime = '2012-04-26 21:57:00';
//Define your endTime here in {Year-Month-Day Hour:Minute:Second} format.
$endTime = '2012-04-27 22:00:00';
$startTimestamp = strtotime($startTime);
$endTimestamp = strtotime($endTime);
$numOfSecondsToReload = $startTimestamp - $currentTimestamp;
if($currentTimestamp >= $startTimestamp && $currentTimestamp <= $endTimestamp):
?>
<a href="*broadcastlink*">LIVE NOW</a>
<?php else: ?>
<p>Live at <?php echo date('H:i', $startTimestamp); ?></p>
<?php header("refresh:$numOfSecondsToReload;$currentPage"); ?>
<?php endif; ?>
UPDATE2:重新加載特定DIV的內容。這裏是你可以使用的代碼。
<div id="live">
<?php
ob_start();
date_default_timezone_set('Europe/London');
$currentPage = $_SERVER['PHP_SELF'];
$currentTimestamp = strtotime(date('Y-m-d H:i:s'));
$startTime = '2012-04-27 12:42:20';
$endTime = '2012-04-28 22:00:00';
$startTimestamp = strtotime($startTime);
$endTimestamp = strtotime($endTime);
$numOfSecondsToReload = $startTimestamp - $currentTimestamp;
if($currentTimestamp >= $startTimestamp && $currentTimestamp <= $endTimestamp):
?>
<a href="*broadcastlink*">LIVE NOW</a>
<?php else: ?>
<p>Live at <?php echo date('H:i', $startTimestamp); ?></p>
<?php endif; ob_end_flush(); ?>
<div id="timeremaining" hidden><?php echo $numOfSecondsToReload * 1000; ?></div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
var numOfMiliSecondsToReload = $('#timeremaining').html();
var timeToReload = (numOfMiliSecondsToReload >= 0) ? numOfMiliSecondsToReload : 86400000;
$(function() {
$(function() { setTimeout(reloadDiv, timeToReload); });
function reloadDiv() { $('#live').load(location.href);}
});
</script>
希望這可以幫助你。
用代碼更新了我的答案。 – 2012-04-26 16:40:23