If statement if current hour is within specific time block

What im attempting to do is check if the current hour is within a specific time block that is different for most days of the week. I have attempted this about 6 different ways and it will do the else once and never again im completely stumped so i thought i’d ask the hive’s help.

Here’s what i’ve currently got.

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

Related posts

Leave a Reply

2 comments

  1. This one should probably do the work for you:

    $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. A good approach can be seen here

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