简体   繁体   English

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

[英]PHP message based on a certain time on different days

I'm trying to make an messages appear on my website when we are open and when the phones are "closed" but I can't seem to get it working. 当我们打开手机以及“关闭”手机时,我试图使一条消息出现在我的网站上,但我似乎无法正常工作。

This is what I have so far (This is my inspiration http://codewalkers.com/c/a/Date-Time-Code/Display-message-according-to-hour-of-day/ ) 这就是我到目前为止所拥有的(这是我的灵感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;
}

?> ?>

and im getting this error message 我收到此错误消息

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

It would be nice to have some qualified help :) 有一些合格的帮助会很不错的:)

The qualified help you requested comes in the form of the script below: 您要求的合格帮助以以下脚本的形式出现:

Also note that in your code you have to use date('l') - lowercase L. 另请注意,在您的代码中,您必须使用date('l')-小写L。

The list of timezones you can use for date_default_timezone_set() can be found here: http://php.net/manual/en/timezones.php 您可以在此处找到可用于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;
}

So perhaps this will solve your problem: 因此,这也许可以解决您的问题:

//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".

And as far as I know, php date() accepts string as first argument. 据我所知,php date()接受字符串作为第一个参数。 And the G and L can only work well if you had earlier defined them as constants ... 而且G和L仅在您之前将它们定义为constants才能正常工作...

EDIT 编辑

Seeing your updated question with the error message, you can try this: 看到包含错误消息的更新问题,您可以尝试以下操作:

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

Or else you could place this in you php configuration file (eg php.ini in windows): 否则,您可以将其放在您的php配置文件中(例如Windows中的php.ini ):

date.timezone = Africa/Lagos

Then, of course, replace "Africa/Lagos" with the timezone of your choice. 然后,当然,用您选择的时区替换“非洲/拉各斯”。 And for a list of supported timezones, see here . 有关受支持的时区的列表, 请参见此处

date() takes a string, and your code could be shorter: 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;

About your warning, open your php.ini and add this block if you don't have it: 关于警告,请打开php.ini并添加此块(如果没有):

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

You can also just add this line at the beginning of your code: date_default_timezone_set('Asia/Seoul'); 您也可以只在代码的开头添加以下行: date_default_timezone_set('Asia/Seoul'); .

See http://php.net/manual/en/timezones.php for the list of supported timezones. 有关受支持的时区列表,请参见http://php.net/manual/en/timezones.php

try this 尝试这个

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

    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM