为了账号安全,请及时绑定邮箱和手机立即绑定

如何在特定时间刷新页面

如何在特定时间刷新页面

PHP
红颜莎娜 2023-04-15 16:44:22
我在我的 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");}?>
查看完整描述

4 回答

?
慕桂英546537

TA贡献1848条经验 获得超10个赞

您不能在 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">


查看完整回答
反对 回复 2023-04-15
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

我用的javascript,功能自动运行


<script>

(function myFunction() {

    var time = new Date().getHours()

    if( time == "02" ) {

    setTimeout(function(){

  window.location = 'your url';

}, 5000); 

   }

     })();

</script>


查看完整回答
反对 回复 2023-04-15
?
蓝山帝景

TA贡献1843条经验 获得超7个赞

您可以将以下代码放在标题中并进行测试。我希望这有帮助。


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

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


    echo "<meta http-equiv='refresh' content='3'>";

}

注意:上面使用的元标记用于定义刷新文档本身的时间间隔。根据需要刷新页面,修改元标记的“content”属性中的值


查看完整回答
反对 回复 2023-04-15
?
鸿蒙传说

TA贡献1865条经验 获得超7个赞

我认为这段代码更好


<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>


查看完整回答
反对 回复 2023-04-15
  • 4 回答
  • 0 关注
  • 109 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信