根据时间显示DIV

时间:2015-05-07 15:59:22

标签: php

我需要根据时间显示“直播新闻”div。 div只会说“看新闻”并成为新闻网站的链接。这是div:

<div class="live-news">Watch the Live News</div>

以下是时间:

Mon - Fri
430am - 8am
noon - 1
5pm - 6:30pm
11 - 11:30pm

Saturday 
7-830am
5-6pm
630-7pm
11-1130pm

Sunday
6-7am
5-6pm
630-7pm
11-12am

如何构建数组,然后从数组中选择以显示元素?

*******更新PHP *******

请帮忙!

2 个答案:

答案 0 :(得分:1)

如果你想在服务器上使用它。

$week_day = date('w');
$current_time = date('Hi');

$show_news_div = false;
if ($week_day >= 1 && $week_day <= 5)
{
  if (($current_time >= '0430' && $current_time <= '0800') || ($current_time >= '1200' && $current_time <= '1300') || ($current_time >= '1700' && $current_time <= '1830') || ($current_time >= '2300' && $current_time <= '2330'))
  {
    $show_news_div = true;
  }
}
if ($week_day == 6)
{
  if (($current_time >= '0700' && $current_time <= '0830') || ($current_time >= '1700' && $current_time <= '1800') || ($current_time >= '1830' && $current_time <= '1900') || ($current_time >= '2300' && $current_time <= '2330'))
  {
    $show_news_div = true;
  }
}

if ($week_day == 0)
{
  if (($current_time >= '0600' && $current_time <= '0700') || ($current_time >= '1700' && $current_time <= '1800') || ($current_time >= '1830' && $current_time <= '1900') || ($current_time >= '2300' && $current_time <= '2359'))
  {
    $show_news_div = true;
  }
}

if ($show_news_div)
{
  ?>
  <div class="live-news">Watch the Live News</div>
  <?php
}

答案 1 :(得分:0)

以下是我采用的最终解决方案:

功能(为了回答而缩短):

function kiroj_live_news() {
$hour = date('G');
$day  = date('N'); // 1..7 for Monday to Sunday

if (($hour >= 2  && $hour <= 24)) { // 2am - midnight
    return '<div class="live-news"><a href="http://www.kirotv.com/videos/news/kiro-newscast-hd2/vCYwYn/" target="_blank">Breaking News</a></div>';
}
}

我在哪里展示了DIV:

<?php echo kiroj_live_news(); ?>

感谢您的帮助。