开发者

Show different message to the hours | php

here is my code, where i make mistake? Btw my time Z开发者_JS百科ONE is UTC + 2:00. Thanks for answers in advance.

<?php

    $current_time = date('H');

    if ($current_time >18) {
    echo "Good night";
    }
    if ($current_time <12) {
    echo "Good morning";
    }
    if (($current_time >=12) && ($current_time <17)) {
    echo "Good day";
    }

?>


$current_time = date('H');

if ($current_time >18) {
echo "Good night";
} else if ($current_time <12) {
echo "Good morning";
} else {
echo "Good day";
}


Your last test should check for $current_time <=17. Note the less than or equal to... if (($current_time >=12) && ($current_time <=17))

@Ernestas Stankevičius has a nice clean solution though.


As predicted, the problem is in time zone.

<?php
    date_default_timezone_set('Europe/Sofia');
    $current_time =date('H');

    echo "$current_time";
    if ($current_time >'18') {
    echo "Good night";
    }
    if ($current_time <'12') {
    echo "Good morning";
    }
    if (($current_time >='12') && ($current_time <='17')) {
    echo "Good day";
    }

?>


Try doing var_dump($current_time);. That way, you will understand what it contains and where you are making a mistake.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜