如果声明当前小时是否在特定时间段内

时间:2013-02-08 14:16:50

标签: php wordpress date if-statement time

我尝试做的是检查当前小时是否在一周中大多数日子不同的特定时间段内。我已经尝试了6种不同的方式,它会做其他的一次,再也不会完全难倒,所以我想我会问蜂巢的帮助。

这是我现在所拥有的。

    /**
     * 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])) {

2 个答案:

答案 0 :(得分:1)

可以看到一个好的方法here

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

答案 1 :(得分: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');
}