【问题标题】:If statement if current hour is within specific time block如果当前时间在特定时间段内的 if 语句
【发布时间】:2013-02-08 14:16:49
【问题描述】:

我试图做的是检查当前时间是否在一周中大部分时间不同的特定时间段内。我已经尝试了大约 6 种不同的方法,它会做一次 else 并且再也不会完全被我难住了,所以我想我会请求蜂巢的帮助。

这是我目前得到的。

    /**
     * Simple Array Storing the times to block on each day
     */
    $nonNewDay = array(
        array(
            '2:00 am',
            '11:00 am'
        ),
        array(
            '2:00 am',
            '11:00 am'
        ),
        array(
            '2:00 am',
            '11:00 am'
        ),
        array(
            '2:00 am',
            '11:00 am'
        ),
        array(
            '2:00 am',
            '11:00 am'
        ),
        // Weekends
        array(
            '2:00 am',
            '1:00 pm'
        ),
        array(
            '2:00 am',
            '12:00 pm'
        ),
    );

    // Get Current weekday and time;
    $weekday = date('w');
    $date = date('m-d-Y');
    $time = date('g:00 a', strtotime('now'));

    echo $date;
    echo '<br />';
    echo $time;
    echo '<br />';

    /**
     * Were Attempting to block out all time blocks that are NOT "New Day Programming"
     * Which currently are:
     * Mon - Fri 11:00 am - 2:00 am
     * Sat, Sun 2:00 am - 1PM
     */

    if (strtotime($date . ' ' . $time) <= strtotime($date . ' ' . $nonNewDay[$weekday - 1][0]) && strtotime($date . ' ' . $time) >= strtotime($date . ' ' . $nonNewDay[$weekday - 1][1])) {

【问题讨论】:

    标签: php wordpress date if-statement time


    【解决方案1】:

    这个应该可以为你完成这项工作:

    $current_date = strtotime(date("g:00 a"));
    $value = $nonNewDay[date('N')-1];
    // for php < 5.1, I think this one will do the work:
    // $value = $nonNewDay[(date('w') == 0 ? 6 : date('N')-1)];
    if ((strtotime($value[0]) <= $current_date) && ($current_date < strtotime($value[1]))) {
        die('Blocked');
    }
    

    【讨论】:

      【解决方案2】:

      好方法可以看here

      $current_time = strtotime('now');
      if ($current_time > strtotime('wednesday this week 8:00pm') && $current_time <    
          strtotime('thursday this week 2:00am')) {
      // do stuff 
      }
      

      【讨论】:

      • 这是否适用于上面数组中的不同时间?没有多个 if else 的?
      • 抱歉,您需要编写多个 if 语句或多个 or 语句。您希望这样做的方式会干净得多。
      • 您可以使用 for 循环遍历数组并用您的值替换日期和时间。
      猜你喜欢
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多