PHP 消息基于不同日期的特定时间

PHP message based on a certain time on different days

我试图在我们开门时和手机开机时在我的网站上显示一条消息"closed",但我似乎无法让它工作。

这就是我目前所拥有的(这是我的灵感http://codewalkers.com/c/a/Date-Time-Code/Display-message-according-to-hour-of-day/

<?php
//Change message of the day
$open = 'We are open for business';
$closed = 'We are closed';

//Get the current time
$current_time = date(G);
//Get the current day
$current_day = date(I);

if ($current_day == "Monday" && $current_time >= 9 && $current_time <= 21) {
    echo $open;
}

elseif ($current_day == "Monday" && $current_time >= 21 && $current_time <= 9) {
    echo $closed; 
}

if ($current_day == "Tuesday" && $current_time >= 9 && $current_time <= 21) {
    echo $open;
}

elseif ($current_day == "Tuesday" && $current_time >= 21 && $current_time <= 9) {
    echo $closed; 
}

if ($current_day == "Wednesday" && $current_time >= 9 && $current_time <= 21) {
    echo $open;
}

elseif ($current_day == "Wednesday" && $current_time >= 21 && $current_time <= 9) {
    echo $closed; 
}

if ($current_day == "Thursday" && $current_time >= 9 && $current_time <= 21) {
    echo $open;
}

elseif ($current_day == "Thursday" && $current_time >= 21 && $current_time <= 9) {
    echo $closed; 
}

if ($current_day == "Friday" && $current_time >= 9 && $current_time <= 19) {
    echo $open;
}

elseif ($current_day == "Friday" && $current_time >= 19 && $current_time <= 9) {
    echo $closed; 
}

if ($current_day == "Saturday") {
    echo $closed;
}

if ($current_day == "Sunday") {
    echo $closed;
}

?>

我收到此错误消息

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in - on line 7 Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in - on line 9

如果能得到一些合格的帮助就好了:)

也许这会解决您的问题:

//Get the current time
$current_time = date('G');
//Get the current day
$current_day = date('l');//This is lowercase 'L' and NOT 'I' as in "India".

据我所知,php date() 接受字符串作为第一个参数。并且 G 和 L 只有在您之前将它们定义为 constants...

才能正常工作

编辑

看到你更新的问题和错误消息,你可以试试这个:

ini_set('date.timezone', 'Africa/Lagos');' //somewhere at the top of your code. 

或者您可以将其放入 php 配置文件中(例如 windows 中的 php.ini):

date.timezone = Africa/Lagos

然后,当然,将 "Africa/Lagos" 替换为您选择的时区。对于支持的时区列表,see here.

date() 接受一个字符串,您的代码可以更短:

//Change message of the day
$open = 'We are open for business';
$closed = 'We are closed';

$current_time = date('G'); //Get the current time
$current_day = date('w'); //Get the current day

if ($current_day > 0 && $current_day < 6) //Monday to Friday
{
  if ($current_time >= 9 && $current_time <= 21 && $current_day != 5) //Monday to Thursday between 9 and 21
    echo $open;
  else if ($current_time >= 9 && $current_time <= 19 && $current_day == 5) //Friday between 9 and 19
    echo $open;
  else
    echo $closed;
}
else //Saturday and Sunday
  echo $closed;

关于您的警告,打开您的 php.ini 并添加此块(如果您没有):

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Seoul

您也可以只在代码开头添加这一行:date_default_timezone_set('Asia/Seoul');.

有关支持的时区列表,请参阅 http://php.net/manual/en/timezones.php

您请求的合格帮助以以下脚本的形式出现:

另请注意,在您的代码中,您必须使用 date('l') - 小写 L。

可在 date_default_timezone_set() 中使用的时区列表可在此处找到: http://php.net/manual/en/timezones.php

date_default_timezone_set('Europe/Amsterdam'); // set it to the right value

$weAreOpen = areWeOpen(date('l'), date('G'));

if($weAreOpen) {
    echo 'We are open for business';
} else {
    echo 'We are closed';
}

/**
 * Test if we're open for business
 * @param string $day - day of week (ex: Monday)
 * @param string $hour - hour of day (ex: 9)
 * @return bool - true if open interval 
 */
function areWeOpen($day, $hour) {
    $hour = (int)$hour;
    switch($day) {
        case 'Monday':
        case 'Tuesday':
        case 'Wednesday':
        case 'Thursday':
            if($hour >= 9 && $hour < 21) {
                return true;
            }
            break;
        case 'Friday':
            if($hour >= 9 && $hour < 19) {
                return true;
            }
            break;
    }
    return false;
}

试试这个

<?php
    date_default_timezone_set('UTC');
    //Change message of the day
    $open = 'We are open for business';
    $closed = 'We are closed';

    //Get the current time
    $current_time = date("G");
    //Get the current day
    $current_day = date("l");

    if ($current_day == "Monday" && $current_time >= 9 && $current_time <= 21) {
        echo $open;
    }

    elseif ($current_day == "Monday" && $current_time >= 21 && $current_time <= 9) {
        echo $closed; 
    }

    if ($current_day == "Tuesday" && $current_time >= 9 && $current_time <= 21) {
        echo $open;
    }

    elseif ($current_day == "Tuesday" && $current_time >= 21 && $current_time <= 9) {
        echo $closed; 
    }

    if ($current_day == "Wednesday" && $current_time >= 9 && $current_time <= 21) {
        echo $open;
    }

    elseif ($current_day == "Wednesday" && $current_time >= 21 && $current_time <= 9) {
        echo $closed; 
    }

    if ($current_day == "Thursday" && $current_time >= 9 && $current_time <= 21) {
        echo $open;
    }

    elseif ($current_day == "Thursday" && $current_time >= 21 && $current_time <= 9) {
        echo $closed; 
    }

    if ($current_day == "Friday" && $current_time >= 9 && $current_time <= 19) {
        echo $open;
    }

    elseif ($current_day == "Friday" && $current_time >= 19 && $current_time <= 9) {
        echo $closed; 
    }

    if ($current_day == "Saturday") {
        echo $closed;
    }

    if ($current_day == "Sunday") {
        echo $closed;
    }

    ?>