0

这是我的代码,我在哪里出错了?顺便说一句,我的时区是 UTC + 2:00。感谢您提前回答。

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

?>
4

4 回答 4

1
$current_time = date('H');

if ($current_time >18) {
echo "Good night";
} else if ($current_time <12) {
echo "Good morning";
} else {
echo "Good day";
}
于 2011-09-25T21:36:52.427 回答
0

尝试做var_dump($current_time);。这样,您将了解它包含的内容以及您在哪里犯了错误。

于 2011-09-25T22:00:35.017 回答
0

您的最后一次测试应该检查$current_time <=17. 注意小于或等于... if (($current_time >=12) && ($current_time <=17))

@Ernestas Stankevičius 有一个不错的干净解决方案

于 2011-09-25T21:37:25.040 回答
0

正如预测的那样,问题出在时区。

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

?>
于 2011-09-26T21:39:54.773 回答