简体   繁体   中英

Display DIV during the news hours using PHP

I have a wordpress site. I need to display a DIV during the news hours. So I have a function in template-functions. It looks like this:

function kiroj_live_news() {
$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;
  }
}
}

In the area I want my DIV to display in, I have an if:

<?php 
        $display = kiroj_live_news ();

        if ($display)
        {
          ?>
            <div class="live-news"><a href="http://www.kirotv.com/videos/news/kiro-newscast-hd2/vCYwYn/" target="_blank">Breaking News</a></div>
          <?php
        }

    ?>

I have changed the times in my main function, tried other date() formats from the PHP manual ( http://php.net/manual/en/function.date.php ) and I cannot get the DIV to display. Can anyone help?

function kiroj_live_news() {

    //your code here

    ....

    return $show_news_div;

}

$display = kiroj_live_news();

......

Your function doesn't return anything - so it's always FALSE .

Here was my final solution (shortened for purpose of this answer):

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>';
}}

And then where I wanted to display my DIV was less complicated:

<?php echo kiroj_live_news(); ?>

Thanks for all of the assistance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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