2011-09-25 4 views
0

여기 내 코드, 내가 실수? Btw 내 시간대는 UTC + 2:00입니다. 미리 답변 해 주셔서 감사합니다.시간과 다른 메시지보기 | PHP는

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

?> 
+0

무엇이 문제입니까? 작동하지 않는 것은 무엇입니까? '$ current_time'에는 무엇이 들어 있습니까? –

답변

1
$current_time = date('H'); 

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

당신의 마지막 테스트는 $current_time <=17 확인해야합니다. 보다 작거나 같음을 유의하십시오. if (($current_time >=12) && ($current_time <=17))

@Ernestas Stankevičius는 nice clean solution입니다.

0

var_dump($current_time);을 시도해보십시오. 그런 식으로, 당신은 그것이 무엇을 포함하고 당신이 실수를하고 있는지 이해하게 될 것입니다.

+0

을 사용하고 if-else 래더를 사용하여 no. 비교 검사 수 – Haywire

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

?>