【问题标题】:Script to redirect to a HTML page between certain times在特定时间之间重定向到 HTML 页面的脚本
【发布时间】:2015-09-18 17:17:59
【问题描述】:

我遇到以下问题:我在 joomla cms 上有一个自定义的 html/php 加载器,它可以加载包含特定信息的页面。此信息每天更新 4 次,时间分别为当地时间 02:00 08:00 14:00 和 20:00。我创建了 4 个页面(每次更新 1 个页面),以便人们可以比较(通过框架)。

现在我想做的是创建一个 html 或 php(如果需要使用 java)页面,根据页面打开的时间重定向到这四个特定页面之一。

如果时间在 02:00 到 08:00 之间,它应该打开“00.html”

如果时间在 08:00 到 14:00 之间,它应该打开“06.html”

如果时间在 14:00 到 20:00 之间应该打开“12.html”

如果时间在 20:00 到 02:00 之间,它应该打开“18.html”

这些 html 文件是框架,其中一个框架包含一个名为“mainmenu”的可用信息菜单,一个包含通过名为“showmodel”的菜单选择的信息的框架和一个用于选择 00/06/12/18如果需要页面。但我希望访问者会在他或她打开主页时获得最新信息......日期无关紧要。重要的是时间。

【问题讨论】:

  • 在 XmlHttpRequest 时代,我看不到框架的用途,但很头疼。
  • 顺便说一下,我们很想看看你到目前为止所做的工作,向我们展示你的一些工作。

标签: java php html datetime


【解决方案1】:

这个例子一定能帮到你:)

    <?php
    $hours = date("H"); // 00 - 23

    if (($hours >= 2) && ($hours <= 8)) {
        header('location: 00.html');
    }

    ?>

【讨论】:

    【解决方案2】:

    由于您希望在多个小时内执行特定重定向,因此您可以使用 switch 语句将其设置得几乎与您想要的一样精细。 switch 语句将检查您的值,然后找到符合条件的“案例”并执行该代码。

    http://php.net/manual/en/control-structures.switch.php

    请记住,在进行重定向时,不能进行其他输出。

    $hour = (int)date('H');
    switch ($hour) {
        case ($hour >= 0 && $hour <= 2):
            header('location: 18.html');
            break;
        case ($hour > 2 && $hour <= 8):
            header('location: 00.html');
            break;
        case ($hour > 8 && $hour <= 14):
            header('location: 06.html');
            break;
        case ($hour > 14 && $hour <= 20):
            header('location: 12.html');
            break;
        case ($hour > 20):
            header('location: 18.html');
            break;
        default:
            header('location: default.html');
            break;
    }
    exit;
    

    重定向文档:http://php.net/manual/en/function.header.php

    【讨论】:

      【解决方案3】:

      没有经过测试,也不完全是问的,但我认为这样的东西应该可以工作

      $hour= date("H");
      header('location: '.$hour.'.html');
      

      只是想指出您可以在header 中使用变量。 以后设置条件可能会更灵活..

      【讨论】:

        【解决方案4】:

        这是我在您的帮助下所做的……如果我没记错的话,它会检查服务器端的时间,对吗?

        <?php
        $hours = date("H"); // 00 - 23
        
        if (($hours >= 0) && ($hours < 2)) {
            header('location: 12.html');
        }
        elseif (($hours >= 2) && ($hours < 8)) {
            header('location: 18.html');
        }
        elseif (($hours >= 8) && ($hours < 14)) {
            header('location: 00.html');
        }
        elseif (($hours >= 14) && ($hours < 20)) {
            header('location: 06.html');
        }
        elseif (($hours >= 20) && ($hours < 0)) {
            header('location: 06.html');
        }
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-09-01
          • 2015-05-09
          • 1970-01-01
          • 1970-01-01
          • 2019-04-18
          • 2014-10-17
          • 2021-04-11
          相关资源
          最近更新 更多