2012-09-18 2 views
0

이 코드를 살펴보고이 수요일 완벽하게 작동이 가능 있다는 것입니다,하지만 얼마나 말해주십시오 화요일에게 :PHP strtotime bug?

<?php 
$current_time = strtotime('now'); 
if ($current_time > strtotime('tuesday this week 8:45pm') && $current_time < strtotime('tuesday this week 11:45pm')) { 
$background = 1; 
} 
if ($current_time > strtotime('wednesday this week 8:45pm') && $current_time < strtotime('wednesday this week 11:45pm')) { 
$background = 1; 
} 
else{ 
$background = 0; 
} 

?> 

    <script> 
    var backday = <?php echo json_encode($background); ?>; 
    </script> 

화요일를 들어, 0을 반환하지만 수요일 것이로, 1을 반환 그것은해야한다. 왜?

+2

설정 한 시간대에 따라 다릅니다. 나에게 그들은 모두 0을 반환해야한다. –

+0

화요일에 실행될 때 이것이 작동하지 않는다는 것을 의미한다고 가정 하겠지만 시간 사이에는 수요일에 실행될 때 작동합니까? – ajon

+0

이 서버를 실행할 때 서버 (로컬이 아님) 시간은 무엇입니까? –

답변

3

로직에 오류가 있습니다. 첫 번째 조건부는 1을 반환 할 수 있지만 두 번째 조건부는 만족합니다. 두 번째 조건부에서 첫 번째 조건이 false이면 첫 번째 조건부에서 설정 한 것과 관계없이 else 블록에서 변수를 0으로 설정합니다. 두 번째 if 문을 다음과 같이 작성해야합니다.

if ($current_time > strtotime('tuesday this week 8:45pm') && $current_time < strtotime('tuesday this week 11:45pm')) { 
    $background = 1; 
} 
else if ($current_time > strtotime('wednesday this week 8:45pm') && $current_time < strtotime('wednesday this week 11:45pm')) { 
    $background = 1; 
} 
else{ 
    $background = 0; 
} 
+0

해결되었으므로, 다른 것을 넣는 것을 잊어 버렸습니다! – gazdac