PHP - 营业时间

时间:2012-05-29 00:13:09

标签: php time

我正在尝试运行一个脚本,该脚本会在营业时间显示一条消息,但是当它过夜时,例如晚上11点至凌晨4点似乎不起作用,我知道如何解决它?目前的代码如下:

date_default_timezone_set('Etc/GMT+0'); 

// Define daily open hours. Must be in 24-hour format, separated by dash. If closed for the day, set to 00:00-00:00
$time_range_mon = '23:00-04:00';
$time_range_tue = '23:00-04:00';
$time_range_wed = '23:00-04:00';
$time_range_thu = '23:00-04:00';
$time_range_fri = '23:00-04:00';
$time_range_sat = '23:00-04:00';
$time_range_sun = '23:00-04:00';

// Gets current day of week
$status_today = strtolower(date("D"));
// Gets current time of day in 00:00 format
$current_time = date("G:i");
// Makes current time of day computer-readable
$current_time_x = strtotime($current_time);

// Builds an array, assigning user-defined time ranges to each day of week
$all_days = array("mon" => $time_range_mon, "tue" => $time_range_tue, "wed" =>     $time_range_wed, "thu" => $time_range_thu, "fri" => $time_range_fri, "sat" =>     $time_range_sat, "sun" => $time_range_sun);
foreach ($all_days as &$each_day) {
    $each_day = explode("-", $each_day);
    $each_day[0] = strtotime($each_day[0]);
    $each_day[1] = strtotime($each_day[1]);
}

// Defines array of possible days of week
$week_days = array("mon", "tue", "wed", "thu", "fri", "sat", "sun");

// Compares current day of week to possible days of week and determines open vs closed     output based on current day and time.
foreach ($week_days as &$each_week_day) {
    if ($status_today == $each_week_day) {
        if (($all_days[$each_week_day][0] <= $current_time_x) &&     ($all_days[$each_week_day][1] >= $current_time_x)) {
        echo "Running Script - Operating Hours";
    } else {
        echo "Not Running Script - Out of Hours";
        }
    }
}

0 个答案:

没有答案