【问题标题】:Show content in a specific time range显示特定时间范围内的内容
【发布时间】:2016-06-28 00:46:31
【问题描述】:

我正在尝试在特定日期和时间(在 Wordpress 中)显示内容,使用:

if (!function_exists('isStreaming')):
    function isStreaming()
{
        date_default_timezone_set('America/Mexico_City');

        $getDateNow = date('Y-m-d');
        $getDayNow  = date('Y-m-d', strtotime($getDateNow));
        $DateBegin  = date('Y-m-d', strtotime("2016-06-28"));
        $DateEnd    = date('Y-m-d', strtotime("2016-06-28"));

        if (($getDayNow >= $DateBegin) && ($getDayNow <= $DateEnd) && is_home()) {
            locate_template(array('/layouts/home/streaming.php'), true);

        } else {
            locate_template(array('/layouts/home/ad-top.php'), true);
        }
    }
endif;

这样可以,内容将显示 7 月 28 日的全天。

但是当我尝试在日期之外添加时间时,它只会向我显示任何一天的时间范围之间的内容,而不考虑日期变量。

更新

我想我终于实现了我想要的:D

<?php
if (!function_exists('isStreaming')):
    function isStreaming()
{
        date_default_timezone_set('America/Mexico_City');

        $getDateNow = date('Y-m-d H:i');
        $getDayNow  = date('Y-m-d H:i', strtotime($getDateNow));
        $DateBegin  = date('Y-m-d H:i', strtotime("2016-06-27 08:00"));
        $DateEnd    = date('Y-m-d H:i', strtotime("2016-06-27 23:15"));

        if (($getDayNow >= $DateBegin) && ($getDayNow <= $DateEnd) && is_home()) {
            echo "YES $DateBegin";

        } else {
            echo "NO $getDayNow";
        }
    }
endif;

echo isStreaming();
?>

感谢您的想法:D

【问题讨论】:

    标签: php wordpress date time


    【解决方案1】:

    好吧,只需使用 time() 并检查它的状态。

    $now = time();
    $start = strtotime("2016-06-28");
    $finish = strtotime("2016-06-29");
    if($now >= $start AND $now < $finish){ 
        locate_template(array('/layouts/home/streaming.php'), true); 
    } else { 
        locate_template(array('/layouts/home/ad-top.php'), true); 
    }
    

    【讨论】:

      【解决方案2】:

      请使用DateTime:

      $Now = new DateTime("now");
      $DateBegin = new DateTime('2016-06-28'); //Add your time !
      $DateEnd = new DateTime('2016-06-28'); //Add your time !
      
      if($DateBegin <= $now && $DateEnd >= $now){
          //is streaming ....
      }
      

      【讨论】:

        猜你喜欢
        • 2017-08-29
        • 2021-10-13
        • 1970-01-01
        • 2023-03-03
        • 2019-09-18
        • 1970-01-01
        • 1970-01-01
        • 2020-07-28
        • 2011-08-12
        相关资源
        最近更新 更多