根据日期和时间以及位置显示打开/关闭

时间:2014-07-02 18:11:50

标签: php datetime

我已经找到了一些我发现的测试示例,并试图将一些答案合并但是没有成功。

这就是我迄今为止所做的事情。我实际上需要在第二个位置添加另一个。

    <?php
     date_default_timezone_set('America/New_York');
     $date = new DateTime;
     echo date("D m/d/y  h:i:s",time())
    ?>

    <?php
    $times = array(
        'mon' => '7:30 AM - 12:00 PM, 12:30 PM - 4:30 PM',
        'tue' => '7:30 AM - 12:00 PM, 12:30 PM - 6:30 PM',
        'wed' => '7:30 AM - 12:00 PM, 3:30 PM - 4:30 PM',
        'thu' => '7:30 AM - 12:00 PM, 12:30 PM - 6:30 PM',
        'fri' => '7:30 AM - 4:30 PM',
        'sat' => '7:30 AM - 12:00 PM',
        'sun' => 'closed'
    );

    function compileHours($times, $timestamp) {
        $times = $times[strtolower(date('D',$timestamp))];
        if(!strpos($times, '-')) return array();
        $hours = explode(",", $times);
        $hours = array_map('explode', array_pad(array(),count($hours),'-'), $hours);
        $hours = array_map('array_map', array_pad(array(),count($hours),'strtotime'),         $hours, array_pad(array(),count($hours),array_pad(array(),2,$timestamp)));
        end($hours);
        if ($hours[key($hours)][0] > $hours[key($hours)][1]) $hours[key($hours)][1] =         strtotime('+1 day', $hours[key($hours)][1]);
        return $hours;
    }

    function isOpen($now, $times) {
        $open = 0; // time until closing in seconds or 0 if closed
        // merge opening hours of today and the day before
        $hours = array_merge(compileHours($times,         strtotime('yesterday',$now)),compileHours($times, $now)); 

        foreach ($hours as $h) {
            if ($now >= $h[0] and $now < $h[1]) {
                $open = $h[1] - $now;
                return $open;
            } 
        }
        return $open;
    }

    $now = strtotime('12:00am');
    $open = isOpen($now, $times);

    if ($open == 0) {
        echo "Closed";
    } else {
        echo "Open";
    }

    ?>

以下是测试页面的链接我感到困惑http://www.sullivanscrap.com/testtime.php任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我相信您的问题是您将$now设置为今天上午的午夜,然后根据您的小时范围进行检查。这将始终返回关闭,因为您的小时范围都不包含该时间。将此更改为$now = time()似乎会产生预期结果。