简体   繁体   English

如果 datetime 设置了条件,则更改 div 内容(不刷新页面)

[英]Change a div content if datetime is set with condition (Without refreshing the page)

I have an issue with my code.我的代码有问题。 My scenario is, let say current DateTime is 2020-06-24 09:15:00 and the wording in div is "Off Air".我的情况是,假设当前 DateTime 是 2020-06-24 09:15:00 并且 div 中的措辞是“Off Air”。 But when the current DateTime is 2020-06-24 10:00:00, the div will change to "On Air" without refreshing the page.但是当当前的 DateTime 是 2020-06-24 10:00:00 时,div 会在不刷新页面的情况下变为“On Air”。 Below is my code下面是我的代码

index.php index.php

<?php

        echo $current = date('d-M-Y H:i A');

        echo "<br>";

        echo $start = '24-Jun-2020 02:56 AM';

        echo "<br>";

        if($start <= $current ) {

        echo '<div>
                <h1 style="color:green;">On Air</h1>

        </div>';

        }else{

            echo '<div>
                <h1 style="color:red;">Off Air</h1>

        </div>';
        }

There are two parts to your problem.你的问题有两个部分。

  1. You need to continuously keep track of the time, and您需要不断跟踪时间,并且
  2. You have to do something once the target time has come around.一旦目标时间到来,你就必须做一些事情。

So basically what you want to do is to fire a function on a set interval and when it runs, check the current date and time.所以基本上你想要做的是在设定的时间间隔内触发 function,当它运行时,检查当前的日期和时间。 Then if your condition is met, you can use Javacript to change the contents of the div.那么如果满足你的条件,就可以使用Javacript来改变div的内容。 remember to set the text back to default when you are outside the target times.当您超出目标时间时,请记住将文本设置回默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM