如何在特定时间刷新页面

我在我的 wp 页面中尝试了这段代码,但它不起作用


<?php

$page = $_SERVER['PHP_SELF'];

$sec = "10";

date("d-m-Y H:i:s");

$time= date("H:i:s");

if($time == "03:40:00")

{

    echo "The new page loading in 10 seconds!";

    header("Refresh: $sec; url=$page");

}

?>


红颜莎娜
浏览 111回答 4
4回答

慕桂英546537

您不能在 PHP 中执行此操作,您可以为此类任务做的是计算时间差并以秒为单位并设置页面的标题以在时间差以秒为单位后刷新,如下所示:<?php$page = $_SERVER['PHP_SELF'];$datetime1 = new DateTime('2020-05-23 18:20:10');$datetime2 = new DateTime('2020-05-23 18:20:30');$interval = $datetime2->diff($datetime2);$diff = $datetime2->getTimestamp() - $datetime1->getTimestamp(); // diff in seconds// you can just have `redirect` queryString params passed like this.if(empty($_GET['redirect'])) {  header("Refresh: $diff; url=$page" . "?redirect=1");}这里要指出的是,使用标记在页面中进行刷新而不是使用header(),作为实现对我来说感觉更清晰,特别是如果您已经有一个模板引擎:<meta http-equiv="refresh" content="20">

蛊毒传说

我用的javascript,功能自动运行<script>(function myFunction() {&nbsp; &nbsp; var time = new Date().getHours()&nbsp; &nbsp; if( time == "02" ) {&nbsp; &nbsp; setTimeout(function(){&nbsp; window.location = 'your url';}, 5000);&nbsp;&nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;})();</script>

蓝山帝景

您可以将以下代码放在标题中并进行测试。我希望这有帮助。if ($time == "03:40:00") {&nbsp; &nbsp; echo "The new page loading in 10 seconds!";&nbsp; &nbsp; echo "<meta http-equiv='refresh' content='3'>";}注意:上面使用的元标记用于定义刷新文档本身的时间间隔。根据需要刷新页面,修改元标记的“content”属性中的值

鸿蒙传说

我认为这段代码更好<script>function refreshAt(hours, minutes, seconds) {    var now = new Date();    var then = new Date();    if(now.getHours() > hours ||       (now.getHours() == hours && now.getMinutes() > minutes) ||        now.getHours() == hours && now.getMinutes() == minutes && now.getSeconds() >= seconds) {        then.setDate(now.getDate() + 1);    }    then.setHours(hours);    then.setMinutes(minutes);    then.setSeconds(seconds);    var timeout = (then.getTime() - now.getTime());    setTimeout(function() { window.location.reload(true); }, timeout);}refreshAt(16,30,0); //Will refresh the page at 4:30pm</script>
打开App,查看更多内容
随时随地看视频慕课网APP