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

时间:2015-03-07 11:01:30

标签: php timezone message

当我们打开手机并关闭手机时,我试图在我的网站上显示一条消息"但我似乎无法让它发挥作用。

这是我到目前为止(这是我的灵感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;
}

&GT;

我正在收到此错误消息

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

获得一些合格的帮助会很高兴:)

4 个答案:

答案 0 :(得分:2)

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

另请注意,在您的代码中,您必须使用日期(&#39; l&#39;) - 小写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;
}

答案 1 :(得分:0)

所以这可能会解决你的问题:

//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()接受字符串作为第一个参数。只有你之前将它们定义为constants ...

时,G和L才能正常工作

修改

使用错误消息查看更新的问题,您可以尝试:

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

否则你可以把它放在php配置文件中(例如windows中的php.ini):

date.timezone = Africa/Lagos

然后,当然,用您选择的时区替换“非洲/拉各斯”。对于支持的时区列表,see here

答案 2 :(得分:0)

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

答案 3 :(得分:0)

试试这个

<?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;
    }

    ?>